jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes Hibernate one-to-many inner join? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Reply locked New topic
Author

Hibernate one-to-many inner join?

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I'm working w/ a one-to-many relationship in Hibernate and while I'm able to retrieve all of my records in one query, it's an outer-join, not an inner-join, which I would prefer.

I'd also prefer to stick w/ Critera queries and not use HQL, if I don't have to, is this all possible?

Thanks!
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Outer, Inner. I am confused so the only way you would get "Outer" results is if the details has orphan records and they are comming into the resultSet. Or a cartisan product. By default join queries in any SQL Hibernate or any other is an inner join.

I don't understand why you are saying you are getting an inner join.

Help.

Thanks

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
I can guarantee you that there are no orphaned records...yet Hibernate is still generating outer joins instead of inner joins...

What do you mean "by default"? Outer joins are quite common, I'd just like to be able to choose when to use them, even if there *were* orphaned records.

Here are my two classes:





Here are the corresponding *.hbm.xml files:





The tables are simple and match the config files...there are no orphaned records in the an_announcement table...they all have a category ID associated w/ them.

Here is the SQL generated by Hibernate (formatted for readability):



Soooooooo? Any ideas? I have "Hibernate in Action" and I see examples of where SQL inner join syntax is used in HQL...but like I said, I'm using Criteria "queries" instead...and would prefer not to use HQL.

Thanks!

-v
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Well, maybe, and I say maybe because I am not sure, but maybe it is because of the one-to-many mapping in the Parent tbale, and not a many-to-one in the child.

That might be the reason why the query is an outer join.

Mark
Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
It does it w/ a many-to-one defined in the child as well. The only reason this example does not have that is because I don't have a getter/setter defined for the parent in the child object....so it doesn't make any sense to populate a field that isn't there (in the child.)

I did some reading through the join association chapter of Hibernate in Action last night (still working my way through the book.) and apparently lazy joins are always going to be outer joins. Otherwise, your option is to use eager fetching which has obvious performance problems should you accidentally load the entire database into memory at one time. I could have said fetch="select" instead of "join" but then it would generate separate select statements for each of the child objects in the list...which is a good example of the "n+1" selects problem...this will perform poorly w/ many objects since you'd be executing many statements vs. one statement.

I'm still working on this...I'll report my progress.

-v
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Thanks for the info Vinnie
Ruprict J. Hollingsfeld
Greenhorn

Joined: Sep 27, 2005
Posts: 2
Have you tried adding:



We had a similar problem with many-to-one associations, and discovered we could explicitly force join strategy - inner using the above syntax, or outer using:

Vinnie Jenks
Ranch Hand

Joined: Apr 26, 2004
Posts: 207
Originally posted by Jim Moon:
Have you tried adding:



We had a similar problem with many-to-one associations, and discovered we could explicitly force join strategy - inner using the above syntax, or outer using:



Actually, coincidentally, I noticed this yesterday while experimenting w/ Associations. Does it have any other adverse effects on the records (other than filtering w/ an inner vs. and outer join?)

Have you been able to perform further filters on child records using this? For an example of what I'm trying to do, read my post here:

http://www.coderanch.com/t/215811/ORM/java/Hibernate-filter-child-collection

I'm trying to figure out if I can filter (or just overall control) a child collection that is being called automatically (lazily) as it is mapped and not called explicitly w/ its own Criteria query.

Thanks Jim!
Kasinath Nalla
Greenhorn

Joined: May 27, 2008
Posts: 2
I am also having the same problem in one-to-many. It still does left outer join.. How did you fix it. Please let me know..
Thanks
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17257
    
    6

Kasinath, please start a new thread for your question, or you can look two replies above yours and see the answer. This thread is almost 3 years old, I am going to close it.

Thanks

Mark
[ May 27, 2008: Message edited by: Mark Spritzler ]
 
jQuery in Action, 2nd edition
 
subject: Hibernate one-to-many inner join?