-->
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.  [ 2 posts ] 
Author Message
 Post subject: Help highly needed with one-to-one mapping
PostPosted: Tue Jul 06, 2010 5:32 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
Hi,

I am running into a much discussed error with a one-to-one mapping, but I just can't getting fixed! I keep running into the following error:

Exception in thread "main" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property [nl.marktmonitor.skillskompas.skillskompas.model.UserSession.user]

I am trying to tie a UserSession to an AbstractUser in a one-to-one relationship using a foreign key association.

This is the mapping for the AbstractUser:

Code:
<hibernate-mapping package="nl.marktmonitor.skillskompas.skillskompas.model">
   <class name="AbstractUser" table="SM_USER">
      <id name="userId" type="integer" column="USER_ID">
         <generator class="native" />
      </id>

      <one-to-one name="userSession" class="UserSession" cascade="all"/>

      <property name="username" type="string" column="USERNAME"
         unique="true" />
      <property name="password" type="string" column="PASSWORD" />

   </class>
</hibernate-mapping>


This is the mapping for the UserSession:

Code:
<hibernate-mapping package="nl.marktmonitor.skillskompas.skillskompas.model">
   <class name="UserSession" table="SM_USERSESSION">
      <id name="sessionId" column="SESSION_ID">
         <generator class="foreign">
            <param name="property">user</param>
         </generator>
      </id>
      
      <one-to-one name="user" class="AbstractUser" constrained="true" />
      
      <property name="UUID" type="string" not-null="true" length="40" column="UUID" />
      <property name="loginDate" type="date" not-null="true" column="LOGIN_DATE" />

   </class>
</hibernate-mapping>


And this is the code I use:
Code:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
AbstractUser user = null;
List userList = session.createQuery("from AbstractUser au where au.username = :username")
   .setString("username", "myUserName")
   .list();

if (userList.size() > 0)
   user = (AbstractUser) userList.get(0);   
      
// Compare (encrypted) passwords
if(user != null && user.getPassword().equals("myPassWord")){
   // Authentication succeeded!
   
   // Create a new UserSession for this user      
   UserSession ses = new UserSession();         
   // Generate the session id
   String uuid = UUID.randomUUID().toString();
   ses.setUUID(uuid);
   ses.setLoginDate(new Date());
   // Set the UserSession on the user
   user.setUserSession(ses);
   // Save/Update
   session.update(user);
   
}
// Commit
   session.getTransaction().commit();   



This throws the "attempted to assign id from null one-to-one property" error. Do you know why and how to solve this? The problem occurs on the session.update(user). All the surrounding code works fine.

Also, when I use session.save() instead of session.update, Hibernate tries to insert a new AbstractUser record with the same id, resulting in a PK constraint failure. Why is Hibernate tryuing to insert a new record instead of just updating the existing one?


Top
 Profile  
 
 Post subject: Re: Help highly needed with one-to-one mapping
PostPosted: Tue Jul 06, 2010 7:08 am 
Newbie

Joined: Fri Jul 02, 2010 9:00 am
Posts: 9
I got it!!!

I forgot to set the AbstractUser on the UserSession before setting the UserSession on the AbstractUser

ses.setUser(user); // do this first
// Set session
user.setUserSession(ses);


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