-->
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.  [ 15 posts ] 
Author Message
 Post subject: One table hierarchy
PostPosted: Tue Mar 02, 2004 1:18 pm 
Newbie

Joined: Fri Feb 27, 2004 9:25 am
Posts: 8
Hi,

I am stuck with having to map a db where there is one table using a parent id column to relate to another record in the same table. This to build a hierarchy.

My question to everyone, is that at all possible to map with hibernate? It would as I understand mean that a class will have a one-to-one (?) relation to itself or am I wrong?

Any help appreciated.

Regards

_________________
Kristofer


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 1:41 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
of course it is possible

this about this:
class Person{
private Long id;
private Integer age;
...
private Person mother;

getter + setter

}

it is not a one-to-one sinci you can have many person for the same mother

it is many(person) to one(mother)

in the mapping file of person you'll have
<many-to-one name="mother" column="ID_MOTHER"/>

ID_MOTHER is the column that references the same table...


Top
 Profile  
 
 Post subject: RE: One table hierarchx
PostPosted: Tue Mar 02, 2004 3:01 pm 
Newbie

Joined: Fri Feb 27, 2004 9:25 am
Posts: 8
Hi,

and thanks for the reply. You are very right it is a many to one relation. Have implemented it the way you suggested and it works fine.

Thx

_________________
Kristofer


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 02, 2004 3:09 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
don't forget to study the outer-join attribute

<many-to-one name="mother" outer-join="TRUE|FALSE" column="ID_MOTHER"/>

take a look at the doc


Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:10 pm 
Newbie

Joined: Mon Mar 15, 2004 3:07 pm
Posts: 8
When you do an HQL find, do you have to explicitly specify the join between the two tables (the same table in this case)? I'm trying to do a query, but the join is not created by default.

Sorry if this is a dumb question.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:15 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
take a look at the doc about all kind of join


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:16 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i forgot, what happen when you call person.getMother()? returns null?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:21 pm 
Newbie

Joined: Mon Mar 15, 2004 3:07 pm
Posts: 8
i've looked at the docs. when you have a many-to-one like Cat - mate, i would think that the join would be created automatically. Does this have something to do with outer-join? I wish I was more familiar with this stuff, so I'm sorry if I sound ignorant.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 15, 2004 3:36 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Go to theserverside.com and read chapter 6 of Hibernate in Action, it is up for public review. There is a section about Joins in this chapter.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 9:47 am 
Newbie

Joined: Mon Mar 15, 2004 3:07 pm
Posts: 8
Ok - I read that chapter, and I finally got my query to work with the following:

select d from Deal d left join d.matchingDeal matchedDeal where d.active = 'Y' and matchedDeal.changed = 'Y'

Obvously I have a Deal object with a reference to a matching Deal object. Mapped as: outer-join="true" lazy="true" (Probably not right???)

Anyway, I thought that Hibernate generated the joins automatically, but I guess not. So does it look like I did it the right way? In the past i've only dealt with collections so this is new to me.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:01 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
select d from Deal where d.active = 'Y' and d.matchingDeal.changed = 'Y'

You can dereference single-ended associations in HQL with a dot, many-to-one is single ended. Hibernate will automatically use an inner join.

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:12 am 
Newbie

Joined: Mon Mar 15, 2004 3:07 pm
Posts: 8
AH Thanks - that's what I was looking for. So if I need a left outer join, I have to do it myself, otherwise the inner join will be generated by default with the many-to-one mapping.

So where does the outer-join mapping come into play when using many-to-one?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:14 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
No, the mapping "outer join" setting is not relevant in HQL, only the multiplicity of your association. I think you have to re-read the chapter again, all I can do is copy/paste :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:17 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
If you have a <many-to-one outer-join="true">, Hibernate will use a single SQL outer join fetch to get both objects during navigational access:

someObject.getBid().getItem() loads data from two tables, the BID and the ITEM. You can also control this behavior with "max_fetch_depth" in the configuration, as outer join is the default for many-to-one!

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 16, 2004 10:33 am 
Newbie

Joined: Mon Mar 15, 2004 3:07 pm
Posts: 8
I get it - I wish I was more technical - maybe I'll buy some hibernate commercial support!!! I have a couple of projects coming up. That works as expected. The problem is when I get a more complicated query like this:

select d from Deal d where d.active = 'Y' and (d.partyA = ? and (d.status in ('B','U') or (d.status in ('F','M','N') and (d.ssiChanged = 'Y' or d.matchingDeal.ssiChanged = 'Y') or (d.completionDate >= ? and d.completionDate < ?))))

This will only work if I make it:

select d from Deal d left join d.matchingDeal matchedDeal ...


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