Meaningless Drivel is fun!*
The moose likes Object Relational Mapping and the fly likes Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6  " Watch "Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6  " New topic
Author

Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6

Herbert Wu
Greenhorn

Joined: Jan 06, 2011
Posts: 3
Hi,
Sorry for the long post as I try to describe the problem context in details.
My problem is to map a relationship table with both regular and composite foreign keys in it.

1 Background:

Assume we have three tables:
social_site(id, site_url)
user(first_name, last_name, birth_day, gender). The PK is composite of first_name and last_name two columns
has_friend(social_site_id, user_first_name, user_last_name, friend_first_name, friend_last_name). The PK is the whole table

Here, the has_friend table defines relationship beteen users for a given social site which basically has three foreign keys:
FK1. social_site_id refers to social_site(id)
FK2/CompositeKey (user_first_name, user_last_name) refers to user(first_name, last_name)
FK3/CompositeKey (friend_first_name, friend_last_name) refers to user(first_name, last_name)

This is the mapping file
----------------------------
<hibernate-mapping>
<class name="HasFriend" table="has_friend">
<composite-id>
<key-many-to-one name="socialSite" class="SocialSite" column="social_site_id"/>

<key-many-to-one name="user" class="User">
<column name="user_first_name"/>
<column name="user_last_name"/>
</key-many-to-one>

<key-many-to-one name="friend" class="User">
<column name="friend_first_name"/>
<column name="friend_last_name"/>
</key-many-to-one>

</composite-id>

</class>

</hibernate-mapping>

2. Problem
In the simple load() call below in which I did not see any SQL generated(show_sql logging is turned on) and the result var "expected" only contains the query instance's id.
So anything wrong with my mapping here? Notice there are two level composite-id here: one at HasFriend class and one at User class.

Other Notes,
(i) The load() did not generate any sql, but getHibernateTemplate().saveOrUpdate(queryInstance) actually generates a proper SELECT statement to check if the row already exists in the table, but it did not generate an insert statement. This test indicates that Hibernate reads the mapping file, but did not always execute it, nor reporting any mapping error.
(ii) I have equals and hashCode and Serializable implemented for HasFriend class.
(iii) For User class mapping(source code not shown here), I used a UserID class to define the composite-id with both firstName and lastName and load() call works fine for a given first and last name.

Thanks for the help
-Herbert

// Construct the query instance
SocialSite queryPara1 = new SocialSite();
queryPart1.setId(100);
User queryPara2 = new user();
queryPara2.setFirstName("John");
queryPara2.setLastName("Doe");
User queryPara3 = new user();
queryPara3.setFirstName("Jane");
queryPara3.setLastName("Doe");

HasFriend queryInstance = new HasFriend();
queryInstance.setSocialSite(queryPara1);
queryInstance.setUser(queryPara2);
queryInstance.setFriend(queryPara3);

// fetch the object from has_friend table
HasFriend expected = getHibernateTemplate().load(HasFriend.class, queryInstance);





Arun Kumarr
Ranch Hand

Joined: May 16, 2005
Posts: 513

Difference between load() and get()


If you are not laughing at yourself, then you just didn't get the joke.
Aniruddh Joshi
Ranch Hand

Joined: Jul 29, 2008
Posts: 275

user(first_name, last_name, birth_day, gender). The PK is composite of first_name and last_name two columns


Don't you think a userid would help ? I have a doubt in case of matching names


Anrd
"One of the best things you could do is to simplify a larger application into a smaller one by reducing its process and complexity - Fowler"
Herbert Wu
Greenhorn

Joined: Jan 06, 2011
Posts: 3
I already have a UserId defined for User class - see my (iiii) comment in the post. The UserId works fine for User class itself.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem in composite key as foreign key mapping w/ hibernate v3.2.6 and jdk-6
 
Similar Threads
Problem with mapping
HBM mapping Problem
Delete/update associations question
hibernate composite key problem
hibernate hello world