-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: OneToMany with inheritance
PostPosted: Wed Jun 23, 2010 4:58 pm 
Newbie

Joined: Tue Jun 01, 2010 12:19 pm
Posts: 3
I would like to find a way to form a OneToMany relationship to objects that are retrieved from the child tables of an inheritance structure. For example I have a table, Shape, that represents the super class of the inheritance structure. In the java class for Shape it includes the annotation @Inheritance(strategy=InheritanceType.JOINED). I then have tables that represent the subclasses of Shape, these include Circle, Rectangle and Triangle. The java classes for each of these extend Shape.

I would like to find a way to form a OneToMany from another table/class called DrawingArea with syntax similar to:

Code:
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "SHAPEID", nullable = false)
    public List<Shape> getShapes() {
        return shapes;
    }


This would return shapes of all types, Circle, Rectangle and Triangle where the primary key SHAPEID matches. How can I do this?


Top
 Profile  
 
 Post subject: Re: OneToMany with inheritance
PostPosted: Thu Jun 24, 2010 1:23 pm 
Newbie

Joined: Tue Jun 01, 2010 12:19 pm
Posts: 3
I haven't gotten any responses to this post yet. It leads me to believe that there may be a limitation in JPA/Hibernate that doesn't allow this type of relationship to be created with annotations. In absence of an annotated solution for this, I have come up with a way to handle it by persisting the elements from the collection manually. In the DrawingArea class I define the collection of Shapes as Transient.
Code:
   private List<Shape> shapes = new ArrayList<Shape>();
.
.
.
   @Transient
   public List<Shape> getShapes() {
      return shapes;
   }

   public void setShapes(List<Shape> value)
   {
      this.shapes = value;
   }


Then in the DrawingAreaDAO, I manually persist each type from the shapes collection:
Code:
    // These values are autowired by Spring, code not included.
    private ShapeDAO shapeDAO;
    private CircleDAO circleDAO;
    private RectangleDAO rectangleDAO;
    private TriangleDAO triangleDAO;
.
.
.   
    @Transactional
    public void persist(DrawingArea transientInstance) {
        entityManager.persist(transientInstance);

        // Save all the shapes.
        List<Shape> shapes = transientInstance.getShapes();
        if (shapes != null) {
            for (Shape shape : shapes) {
               if (Circle.class.isAssignableFrom(shape.getClass())) {
                  circleDAO.persist((Circle)shape);
               } else if (Rectangle.class.isAssignableFrom(shape.getClass())) {
                  rectangleDAO.persist((Rectangle)shape);
               } else if (Triangle.class.isAssignableFrom(shape.getClass())) {
                  triangleDAO.persist((Triangle)shape);
               } else {
                  shapeDAO.persist(shape);
               }
            }
        }
    }


I would be very interested to hear comments about this implementation and/or suggestions to improve it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.