Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to select child object without his parent

 
Wojtek Baczyński
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me with simple crap?
I have many-to-one relation, Objects (parent) with really huge list of childs. Now when i do select of one particular child, hibernate is selecting also a parent. And becouse parent have many childrens, i get also many childrens.
But i want only this one child without a parent and others. What to do? Sure I can ingore other that but when list have 1000+ elements that is huge waste.
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tantummano please check your private messages for some administrative matter...
 
Ankit Garg
Sheriff
Posts: 9580
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can configure the parent property in child class to be lazy loaded, that way it won't be loaded automatically...
 
Wojtek Baczyński
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit thanks for your help but i've tried putting lazy="true" into many wird places and it never helps so could you be more specific? Please look at my simple files:

Site is parent and it have fields: Id, Name
Alias id child and it have: Id, SiteId, Name

So i don't know how to put lazy properitie to Alias file. I have lazy="true" in "<bag ..." in parent but it didn't seem to make any difference.
Tell me what should I change?



 
Ran Pleasant
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wojtek Baczyński wrote:Can anyone help me with simple crap?
I have many-to-one relation, Objects (parent) with really huge list of childs. Now when i do select of one particular child, hibernate is selecting also a parent. And becouse parent have many childrens, i get also many childrens.
But i want only this one child without a parent and others. What to do? Sure I can ingore other that but when list have 1000+ elements that is huge waste.

This is actually a common design problem that is a performance killer. In select cases a child object does need to have a reference to its parent object. However, in the majority of cases there is not a *real* need for a child object to have a reference to the parent object. Circilar references between classes can also set you up for an endless loop. I would suggest taking a step back from your code, reviewing your design, and ask yourself if the child object really does needs a reference to the parent object.
 
Wojtek Baczyński
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think child don't need reference to parent at all. Simple ID (Foreign Key) would do the trick. But i thought that this lauzy reference is required to do one-to-many relation.

Clearly i don't like DB at all so forgive me if i am being noob.

I found something like solution i think. When i did that:


Then I got two select's, one with all aliases from site and one with site.
But if i changed it that way:


It gives only select to alias.
alias.ToString() is ovveriden by me and it is printing also parent field Site.Name
In second code which i pasted i of course got exception that I can't get into Site couse of lazy loading.
So it is kinda solution, i got only alias without site and other aliases.
 
rakesh kanchalwar
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wojtek Baczyński wrote:Can anyone help me with simple crap?
I have many-to-one relation, Objects (parent) with really huge list of childs. Now when i do select of one particular child, hibernate is selecting also a parent. And becouse parent have many childrens, i get also many childrens.
But i want only this one child without a parent and others. What to do? Sure I can ingore other that but when list have 1000+ elements that is huge waste.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic