-->
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: Hibernate and the composite pattern
PostPosted: Tue Mar 16, 2010 11:49 pm 
Newbie

Joined: Tue Mar 16, 2010 11:31 pm
Posts: 2
Hello everyone,

I have the following issue, that I already spent a couple of hours on:

Scenario:

I have a class Person that is subclassed into father and son.
The Person class holds a list of Person class (all the relations a Person has)

Code:
public class Person{
//yada yada yada
private List<Person> children = new List<Person>
private List<Person> parents = new List<Person>

   @ManyToMany(fetch=FetchType.LAZY)
   @IndexColumn(name = "children_INDEX")
   public List<Person> getChildren() {
      return children;
    }

    public void setChildren(List<Person> children) {
      this.children= children;
    }


    @ManyToMany(mappedBy="children", fetch=FetchType.LAZY)
    public List<Person> getParents() {
        return parents;
    }

    public void setParents (List<Person> parents ) {
        this.parents = parents ;
    }

//yada yada yada
}


The mapping is working as expected, however when obtaining the list of parents or children, the individual entities are always of type Person and never of the correct subtype (i.e father or son).

I am using the inheritance strategy joined.

Any suggestions of how to have the lists populated with the actual subclasses are highly appreciated.

Thanks,
Steve


Top
 Profile  
 
 Post subject: Re: Hibernate and the composite pattern
PostPosted: Wed Mar 17, 2010 4:22 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Beside the fact that there is lack of information about how you defined the classes father and son
and HOW you fill the relations concretely, your mapping is weak because

Code:
setChildren(List<Person> children)


even would accept fathers as childs.
Better would be

Code:
setChildren(List<Child> children)


where class Child extends class Person.
The same thing should be considered for the parents.


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.