-->
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: one-to-many without back link attribute?
PostPosted: Tue Oct 25, 2005 3:27 pm 
Newbie

Joined: Tue Oct 25, 2005 3:13 pm
Posts: 4
I wonder if it is possible to create a one-to-many mapping without an attribute linking back.

In Java it is common to use containers of objects. The objects know nothing of the object holding the container. I know that the DB table needs a column to link from B to A. Do I need to do that in my Java classes too? Is it possible to map the following class without adding a A attribute to my Java class?

Code:
class A {
  Set<B> set;
}
class B {
}


Regards
Haug


Top
 Profile  
 
 Post subject: Re: one-to-many without back link attribute?
PostPosted: Tue Oct 25, 2005 3:38 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
pinus wrote:
I wonder if it is possible to create a one-to-many mapping without an attribute linking back.

In Java it is common to use containers of objects. The objects know nothing of the object holding the container. I know that the DB table needs a column to link from B to A. Do I need to do that in my Java classes too? Is it possible to map the following class without adding a A attribute to my Java class?

Code:
class A {
  Set<B> set;
}
class B {
}


Regards
Haug


If you don't have an A attribute on your B Object, how will you insert a new B and identify it as belonging to A ?

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 3:11 pm 
Newbie

Joined: Tue Oct 25, 2005 3:13 pm
Posts: 4
This is simple, the OR mapper adds a reference column to the B-table (b_Id) without adding an attribute to the B-object. The get operation selects all from b-table with b_Id = a_Id. The set operation could be implemented similar.

Your question answers my original question, Hibernate forces the relational model to the object model. The object model uses a uni directional one-to-many relationship. The relational model doesn't have such a thing directly, you have to "emulate" it. And Hibernate doesn't do that. That's the answer to my original question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 4:15 pm 
Beginner
Beginner

Joined: Thu Jan 22, 2004 8:22 pm
Posts: 48
The mapping you propose is legal and actually works. I just modified one of my small test programs to match that mapping and was able to both save and retreive data. Hibernate has no objections to uni-directional associations though I seem to remember a comment in the manual that in SOME cases they might not perform well.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 12, 2005 9:39 am 
Newbie

Joined: Tue Oct 25, 2005 3:13 pm
Posts: 4
Can you please post some mor information how this mapping is done?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 12, 2005 11:18 pm 
Newbie

Joined: Fri Nov 04, 2005 3:04 pm
Posts: 15
JustKeith wrote:
The mapping you propose is legal and actually works. I just modified one of my small test programs to match that mapping and was able to both save and retreive data. Hibernate has no objections to uni-directional associations though I seem to remember a comment in the manual that in SOME cases they might not perform well.


I tried doing it but was unable to insert into child table! how did u do that?/


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 13, 2005 7:25 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Uni-directional and bi-directional is possible (though in most cases your better off using bi-directional -- see manual for explanation).

In a uni-directional mapping the parent mapping of the set is the same as bi other than inverse="false" (or leave it out) attribute. The child will not have the many-to-one mapping. In both cases the DDL is the same.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 2:50 am 
Beginner
Beginner

Joined: Mon Aug 15, 2005 4:37 pm
Posts: 27
Location: Washington DC
letap wrote:
JustKeith wrote:
The mapping you propose is legal and actually works. I just modified one of my small test programs to match that mapping and was able to both save and retreive data. Hibernate has no objections to uni-directional associations though I seem to remember a comment in the manual that in SOME cases they might not perform well.


I tried doing it but was unable to insert into child table! how did u do that?/


I have had this issue while deleting. I tried to delete the parent with a mapping on the parent class link to the child as
<set
name="childProps"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="PARENT_ID" />
</key>
<one-to-many
class="ChildClass"
/>
</set>

and it tries to delete from the parent without deleting the children which causes the database to throw a child record found error because the child not being deleted.

Thanks for any help!!

Surya


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 14, 2005 3:57 am 
Regular
Regular

Joined: Sat Nov 05, 2005 5:33 am
Posts: 70
Location: Linz, Austria
suryavijayk wrote:
letap wrote:
JustKeith wrote:
The mapping you propose is legal and actually works. I just modified one of my small test programs to match that mapping and was able to both save and retreive data. Hibernate has no objections to uni-directional associations though I seem to remember a comment in the manual that in SOME cases they might not perform well.


I tried doing it but was unable to insert into child table! how did u do that?/


I have had this issue while deleting. I tried to delete the parent with a mapping on the parent class link to the child as
<set
name="childProps"
lazy="true"
inverse="true"
cascade="all"
>
<key>
<column name="PARENT_ID" />
</key>
<one-to-many
class="ChildClass"
/>
</set>

and it tries to delete from the parent without deleting the children which causes the database to throw a child record found error because the child not being deleted.

Thanks for any help!!

Surya


I'm not sure, but maybe cascade="all,delete-orphans" might help.
The question that arises is whether a child object that is not in the set any more should be deleted. I don't think that Hibernate does that per default.

Reference Chapter 11 wrote:
If the child object's lifespan is bounded by the lifespan of the of the parent object make it a lifecycle object by specifying cascade="all,delete-orphan".


hth,
Heinz
Don't forget to rate if this helped


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.