-->
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.  [ 9 posts ] 
Author Message
 Post subject: Basic Parent/Child Question
PostPosted: Sun Feb 17, 2008 6:33 pm 
Newbie

Joined: Thu Feb 22, 2007 11:47 pm
Posts: 11
If I have the following code and XML mapping, should I keep the "Parent.setChildren()" method since "inverse=true" for this Parent class? Hibernate will never use this code/side of association, correct? I'm thinking only Child side code will be used to make the association, but want to make sure I can get rid of the Parent side setter for the association since "inverse=true" (and let Child.setParent() take care of making the association.

Thanks a bunch!

Java code:

public class Parent {
private Set<Child> children = new HashSet<Child>();

public void setChildren(Set<Child> children) {
this.children = children;
}

public void addChild(Child child) {
child.setParent(this);
children.add(child);
}
}

public class Child {
private Parent parent;

public void setParent(Parent parent) {
this.parent = parent;
}
{


Hibernate XML Mapping (for both Parent and Child):

<hibernate-mapping>
<class name="Parent" table="PARENT">
<set name="children" inverse="true" cascade="all">
<key column="PARENT_ID"/>
<one-to-many class="Child"/>
</set>
</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="Child" table="CHILD">
<many-to-one name="parent" column="PARENT_ID" class="Parent" not-null="true"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Re: Basic Parent/Child Question
PostPosted: Sun Feb 17, 2008 6:48 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
True. You don't need the parent side's collection in order to save the relation in database; however, you would need the collection if you need the relation to be saved by reachability from a parent instance (cascading....) and if you need to have a list of children for a parent.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 17, 2008 9:47 pm 
Newbie

Joined: Thu Feb 22, 2007 11:47 pm
Posts: 11
Thanks farzad. Unfortunately, I get the following exception if I remove Parent.setChildren(). I think Hibernate still needs the setter for "setting" the list of children even when the Parent <set> is "inverse=true", do you know why? I thought because Parent <set> "inverse=true", Hibernate would never use Parent.setChildren()? Thanks.

Exception in thread "main" java.lang.ExceptionInInitializerError
at util.HibernateUtil.<clinit>(HibernateUtil.java:18)
at ideal.exeParentChildCreateReadDelete.delete(exeParentChildCreateReadDelete.java:70)
at ideal.exeParentChildCreateReadDelete.main(exeParentChildCreateReadDelete.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:90)
Caused by: org.hibernate.PropertyNotFoundException: Could not find a setter for property children in class ideal.Parent
at org.hibernate.property.BasicPropertyAccessor.createSetter(BasicPropertyAccessor.java:216)
at org.hibernate.property.BasicPropertyAccessor.getSetter(BasicPropertyAccessor.java:209)
at org.hibernate.mapping.Property.getSetter(Property.java:277)
at org.hibernate.tuple.entity.PojoEntityTuplizer.buildPropertySetter(PojoEntityTuplizer.java:251)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.<init>(AbstractEntityTuplizer.java:126)
at org.hibernate.tuple.entity.PojoEntityTuplizer.<init>(PojoEntityTuplizer.java:55)
at org.hibernate.tuple.entity.EntityEntityModeToTuplizerMapping.<init>(EntityEntityModeToTuplizerMapping.java:56)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:302)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:434)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:109)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:226)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1300)
at util.HibernateUtil.<clinit>(HibernateUtil.java:16)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 17, 2008 9:50 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
You do need to remove the mapping too. It will be a unidirectional mapping.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 17, 2008 10:11 pm 
Newbie

Joined: Thu Feb 22, 2007 11:47 pm
Posts: 11
Thanks again. I need bidirectional so I'll just leave it. Just thought even with bidirectional I could just keep getChildren and remove setChildren givne "inverse=true". Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 17, 2008 11:48 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
ginchez wrote:
Thanks again. I need bidirectional so I'll just leave it. Just thought even with bidirectional I could just keep getChildren and remove setChildren givne "inverse=true". Thanks.



I am not sure if you are on the right path. inverse="true" has nothing to do with the collection and its accessors. It only designates who own the relationship, which indicates which objects causes saving the relationship in database. I would expect problems popping up if you remove the setter method.



Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 1:52 am 
Newbie

Joined: Thu Feb 22, 2007 11:47 pm
Posts: 11
I thought "inverse=true" determined which class assessors Hibernate uses to manage the association? In other words, as you state inverse="true" designates which class owns the relationship. Thus, wouldn't this mean only the owner class setters need to be present for Hibernate to use (since Hibernate will never use the setters of the class that does not own the relationship)? If so, this is why I thought I could remove the assessors of the class that does not own the relationship.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 11:26 am 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
there are two things, the side with the inverse=false owns the relation and has to have the property. the other side is not necessary and can be completely removed (mapping + java code). However, if you need to access the relationship from both end you need to define the property on the both ends. The second thing is then what inverse is doing here. Imagine this example:

Code:
Parent p1 = new Parent();
Parent p2 = new Parent();
Child c1 = new Child();

c1.setParent(p1);
p2.getChildren().add(c1);
....
save(p1);
save(p2);


The parent of c1 will be set to p1 if the relationship is owned by c1 and even though c1 is added to the children list of p2 that doesn't make a difference. I am not sure if hibernate complains for these cases.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 18, 2008 12:08 pm 
Newbie

Joined: Thu Feb 22, 2007 11:47 pm
Posts: 11
Exactly, which is why I was thinking you could remove "p2.getChildren()" (getter) from the Parent class, correct? Since as you say "though c1 is added to the children list of p2 that doesn't make a difference".

This was my original question, sorry if I wasn't clear to begin with.

Thanks again Farzad.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.