jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes How to select child object without his parent Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to select child object without his parent" Watch "How to select child object without his parent" New topic
Author

How to select child object without his parent

Wojtek Baczyński
Greenhorn

Joined: Jul 28, 2010
Posts: 3
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

Tantummano please check your private messages for some administrative matter...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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

Joined: Jul 28, 2010
Posts: 3
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

Joined: Jan 16, 2003
Posts: 75
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.


Ran
Wojtek Baczyński
Greenhorn

Joined: Jul 28, 2010
Posts: 3
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

Joined: Jul 29, 2010
Posts: 3
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.
 
 
subject: How to select child object without his parent