• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);





 
Ranch Hand
Posts: 662
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Difference between load() and get()
 
Ranch Foreman
Posts: 275
jQuery Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Herbert Wu
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I already have a UserId defined for User class - see my (iiii) comment in the post. The UserId works fine for User class itself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic