-->
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.  [ 8 posts ] 
Author Message
 Post subject: one-to-one relationship
PostPosted: Thu Dec 18, 2008 11:29 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hello everyone,

I am trying to get a one-to-one relationship working, but i am stuck at this.

I have two tables, user and profile. One user can only have one profile, and one profile belongs only to one user.

These are part of my mapping files:

User
<many-to-one name="profile" cascade="all" class="com.gacms.model.user.web.Profile" column="id_profile" unique="true"/>

Profile
<one-to-one name="user" class="com.gacms.model.user.web.User" property-ref="profile" />

When i insert an user, also the profile is inserted correctly. But if i try to insert another user, it is inserted ok, but the last profile gets updated instead of inserting a new one.

Can someone help me? i cannot get it working.

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 1:34 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
This is the method to make the user (and its profile) persistent:

Code:
public void makePersistent(User user) throws InfrastructureException {
      try {   
         HibernateUtil.beginTransaction();
         HibernateUtil.getSession().merge(user);
         HibernateUtil.commitTransaction();
      } catch (HibernateException ex) {
         HibernateUtil.rollbackTransaction();
         throw new InfrastructureException(ex);
      } finally {
         HibernateUtil.closeSession();
        }
   }


If i call merge, the profile is insserted correctly, but if i call save, the profile is updated. Why is that happening? isnĀ“t save the right way to persist objects?

Thanks in advance for any answer.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 5:48 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
The mappings seems to be ok. I don't know why merge() and save() behaves differently. How are you creating the User and Profile objects? Are you creating new instances the second time? Which one of the users references the (single) Profile afterwards?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 18, 2008 7:53 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Thanks for the response.

Yes, i am creating new instances everytime in my insert servlet
Code:
User user = new User();
Profile profile = new Profile();


Then i add data to both instances and make the user reference the profile

Code:
user.setProfile(profile);


and i finally call my
Code:
webUserDao.makePersistent(user);


I cannot see something wrong there...any idea?

Thanks again


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 3:57 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
What you have shown so far seems to be ok. What about the ID generation strategy you are using? Could it be that the second 'new Profile()' happens to give the Profile object the same ID as the first?

I think it is time to enable some debugging options and see what Hibernate is doing.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 7:10 am 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hi again nordborg,

this is the id generation part in my Profile.hbm:
Code:
<id name="id" type="integer" column="id">
      <generator class="sequence">
         <param name="sequence">gatalents_profile_id_seq</param>
      </generator>
   </id>


My database is postgreSQL and have an autoincrement field for my id.

Could it be the id initialization in my pojo?
Code:
public class Profile implements Serializable {
   
   private int id = 0;

....


Or maybe a wrong implementation of the equals, hashCode, toString and compareTo methods?
Code:
public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof Profile)) return false;

      final Profile profile = (Profile) o;

      if (creationDate != null ? !creationDate.equals(profile.creationDate) : profile.creationDate != null) return false;
      if (dni != null ? !dni.equals(profile.dni) : profile.dni != null) return false;

      return true;
   }

   public int hashCode() {
      int result;
      result = (dni != null ? dni.hashCode() : 0);
      result = 29 * result + (creationDate != null ? creationDate.hashCode() : 0);
      return result;
   }

   public String toString() {
      return  "Profile ('" + getId() + "'), " +
            "Dni: '" + getDni() + "'";
   }
   
   public int compareTo(Object o) {
      if (o instanceof Profile) {
         return this.getDni().compareTo( ((Profile)o).getDni() );
      }
      return 0;
   }


Thank you very much for your help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 19, 2008 7:31 am 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
Could it be the id initialization in my pojo?


Have you tried setting <id ... unsaved-value="0" ..>? What ID do you get on the profile that is saved to the database, 0 or something else?

Quote:
Or maybe a wrong implementation of the equals, hashCode, toString and compareTo methods?


I don't think Hiberante uses these methods when saving... but just to be sure it may be a good idea to put in some debug statments to see what happens. Have you checked that the two Profile objects you are creating are not equal? Is it correct to assume that the 'creationDate' and 'dni' is supposed to make up a unique value for all Profile objects? The compareTo() method seems to be inconsistent with equals() but you may have a reason for it and I don't think it should make any difference.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 12:25 pm 
Beginner
Beginner

Joined: Mon May 26, 2008 3:34 am
Posts: 31
Hi again nordborg,

Sorry for being so late. I have been doing some testing and i think hibernate thinks that the two Profile objects are equals.

if i print the creationDate before calling insert, i get the same exact value for both objects.

This is the sql debug the first time i make an insert:

Code:
Hibernate: select this_.id as id1_0_, this_.login as login1_0_, this_.password as password1_0_, this_.creation_date as creation4_1_0_, this_.id_profile as id5_1_0_, this_.id_category as id6_1_0_ from gatalents_user this_ where this_.login=?
Tue Dec 23 17:23:24 CET 2008
Hibernate: select nextval ('gatalents_user_id_seq')
Hibernate: select nextval ('gatalents_profile_id_seq')
Hibernate: select nextval ('gatalents_resource_id_seq')
Hibernate: insert into gatalents_profile (name, first_surname, second_surname, dni, birth_date, id_gender, id_province, id_country, city, zip_code, address, first_phone_number, second_phone_number, email, web_link, description, creation_date, id_category, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into gatalents_user (login, password, creation_date, id_profile, id_category, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into gatalents_resource (title, description, file_name, file_path, creation_date, id_category, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update gatalents_resource set id_user=? where id=?
Hibernate: select this_.id as id1_0_, this_.login as login1_0_, this_.password as password1_0_, this_.creation_date as creation4_1_0_, this_.id_profile as id5_1_0_, this_.id_category as id6_1_0_ from gatalents_user this_


and this is the sql generated the second time:

Code:
Hibernate: select this_.id as id1_0_, this_.login as login1_0_, this_.password as password1_0_, this_.creation_date as creation4_1_0_, this_.id_profile as id5_1_0_, this_.id_category as id6_1_0_ from gatalents_user this_ where this_.login=?
Tue Dec 23 17:23:24 CET 2008
Hibernate: select nextval ('gatalents_user_id_seq')
Hibernate: select nextval ('gatalents_resource_id_seq')
Hibernate: insert into gatalents_user (login, password, creation_date, id_profile, id_category, id) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into gatalents_resource (title, description, file_name, file_path, creation_date, id_category, id) values (?, ?, ?, ?, ?, ?, ?)
Hibernate: update gatalents_profile set name=?, first_surname=?, second_surname=?, dni=?, id_gender=?, id_province=?, id_country=?, city=?, zip_code=?, address=?, first_phone_number=?, second_phone_number=?, email=?, web_link=?, description=?, id_category=? where id=?
Hibernate: update gatalents_resource set title=?, description=?, file_name=?, file_path=?, id_category=? where id=?
Hibernate: update gatalents_resource set id_user=? where id=?
Hibernate: update gatalents_resource set id_user=? where id=?
Hibernate: select this_.id as id1_0_, this_.login as login1_0_, this_.password as password1_0_, this_.creation_date as creation4_1_0_, this_.id_profile as id5_1_0_, this_.id_category as id6_1_0_ from gatalents_user this_


Any other idea? thanks for being so helpful


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