-->
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.  [ 7 posts ] 
Author Message
 Post subject: how to join on other then primary key?
PostPosted: Mon Apr 26, 2004 5:42 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
Hello.

I have these two tables:

Person
personID
personName
personAddress
CountryID
GroupID
houseID

Group
groupID
groupName

Now, what I want to do, is to select a person's ID, its name, and join this with Group, so that I'll get a resultset like this:

Code:
personID   personName             groupID     groupName
1               john                1              groupONE
2               james            (null)
3               carl                (null)
7               dan                2               groupTWO
8               joe                (null)
10             paul                (null)

I am using a join like this:

select p.personID,p.personName,Group from Person p left JOIN p.Group as Group


What I seem to not understand, is how I can map or join two tables that are connected through other things than the primary key.
Right now my mapping file for "person" looks something like this:

<hibernate-mapping >
<class name="Person" table="Person" >
<id name="personID" column="personID" type="int" unsaved-value="0">
<generator class="identity"/>
</id>

<property name="personName" column="personName" type="java.lang.String" />
<property name="personAddress" column="personAddress" type="java.lang.String" />

<property name="CountryID" column="CountryID" type="int" />
<property name="GroupID" column="GroupID" type="int" />
<property name="houseID" column="houseID" type="int" />

<set name="Group" table="Group" inverse="true" cascade="all" lazy="true">
<key column="GroupID" />
<one-to-many class="Group" />
</set>
</class>
</hibernate-mapping>



This gives me the problem that the join will be performed on Group.GroupID=Person.personID, which is not what I want.

What I want is a join on Group.GroupID=Person.GroupID .... I am sure that it is my mapping file that is incorrect, but I have no idea what it should look like in order to be able to create a join like this.

P.S : There might be errors in the code above, it has been simplified / examplified from the original in order to make it a little more clear what I'm after.

Regards C.H


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 5:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
the only question you have to answer is; it is
- a one(group) to many (person) relation
- or a one(person) to many (group) relation

or maybe it is a many (person)to many(group)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 7:16 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
Uhm... that did not really help me at all.
One Group can be in many Persons:
<one-to-many class="Group" />

If that was supposed to be a solution , I did not understand it..

Could you give me an example on how the hibernate query would look like?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 7:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
i'm afraid i do not understand if this is really a query problem or a mapping problem

Quote:
One Group can be in many Persons

--> you are thinking about person mapping file, so in this point of view, you only have one group for one person, so in the person mapping file you'll have a <many-to-one name="group" class="Group"/>

when loading a particular person, you will be able to retrieve its associated group, by calling myPerson.getGroup()


Top
 Profile  
 
 Post subject: problems getting an object from query
PostPosted: Mon Apr 26, 2004 8:34 am 
Newbie

Joined: Fri Apr 09, 2004 9:21 am
Posts: 9
Hello.

I've tried that. The problem I'm having, is that When I choose to select only a few of the fields / properties , and not the whole object, I can't seem to be able to cast it to a Person object. I have to select a whole person in order to do this, and I do not need all of the fields / properties a person consists of.



if I do "select t.field1,t.field2 from table as t" , I am not able to cast it to a Person object...

If you think that this should be possible, could you please give me an example on how you mean this should be done.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 8:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
if you want to cast into a person, you'll need at least all "simple" properties (int, string...), for the other properties (objects like group or list) you can lazy load them using lazy = true and proxy


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 9:47 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
What you are doing in your select is a projection, which will give you a list of Object[]. If you want Person objects, query for them. Please read the docs at http://www.hibernate.org/hib_docs/reference/en/html/queryhql.html#queryhql-select


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