-->
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.  [ 4 posts ] 
Author Message
 Post subject: many to one relation org.hibernate.TransientObjectException
PostPosted: Tue Jun 16, 2009 6:21 am 
Newbie

Joined: Sun Mar 01, 2009 11:11 pm
Posts: 9
Hi,

I using Spring and hibernate and when i try to save a detached object which has many to one relation with the other class, I get org.hibernate.TransientObjectException


I am new to hibernate, so please correct me if i am wrong.

Employee class
Code:
public class Employee implements Serializable
{
   
   private long  EPMST_EMP_ID;  // primary key
    //  other columns
}


Each Employee may have a user Detail (It is not mandatory to have a user Detail )
Code:
public class MyUserDetails implements UserDetails
{
    private String username;
    private String password;
    private boolean accountNonExpired;
    private boolean accountNonLocked;
    private boolean credentialsNonExpired;
    private boolean enabled;

    private long UPMST_EMP_ID;    // FK this is related to Employee class PK
   private Employee UPMST_EMP;   // property of the type Employee Class ( Each userDetail will have a Employee)
}

The above class is not implementing Serializable (I don't know if it will make any difference), but the UserDetails is a spring security Interface.


MyUserDetails mapping

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.general.model.MyUserDetails" table="CMUPMST_TB">
      <id name="username" column="UPMST_USER_CODE" type="java.lang.String"
         unsaved-value="0">
         <generator class="assigned" />
      </id>
      <property name="password" column="UPMST_USER_PWD"
         type="java.lang.String"  />
      <property name="accountNonExpired" column="UPMST_ACCNT_NEXP"
         type="boolean" />
      <property name="accountNonLocked" column="UPMST_ACCNT_NLOC"
         type="boolean" />
         
               <property name="credentialsNonExpired" column="UPMST_CREDN_NEXP"
         type="boolean" />
         
               <property name="enabled" column="UPMST_ENABLED"
         type="boolean" />
      <property name="UPMST_ROLE" column="UPMST_ROLE"
         type="java.lang.String" />    
      
   <many-to-one  name="UPMST_EMP"   column="UPMST_EMP_ID"  fetch="join"  cascade="none" entity-name="hmil.com.general.model.Employee"  />
      

   </class>
</hibernate-mapping>


Employee Mapping

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>


   <class name="hmil.com.general.model.Employee" table="CMEPMST_TB">
      <id name="EPMST_EMP_ID"   type="long" unsaved-value="0">
         <generator class="native">      
         <param name="sequence">CMEPMST_SEQ</param>
         </generator>
      </id>
      
//other column mapping   
   </class></hibernate-mapping>



Now I select the UserDetail and send the object to client (Which is flex in my case , thru blazeds), So now i think it is a detached object

Code where I select the userdetails (I have remove the common statements to look simple)
Code:
Session session = HibernateUtils.currentSession();
Criteria criteria = session.createCriteria(MyUserDetails.class).add(Restrictions.eq("username", userId));
myUserDetailsObj = (MyUserDetails) criteria.uniqueResult();
finally{session.clear();}
return myUserDetailsObj;


I edit the UserDetail and send it back to update , In my client side,when i am editing the user detail, I cannot edit the Employee on same screen, but i can select a different employee for this user, (basically you change the FK but the Employee is not edited, but is is Readonly , display field)

Now when i save the UserDetail
Code:
Session session = HibernateUtils.currentSession();
session.beginTransaction();
session.save(myUserDetails);
session.getTransaction().commit();


Now i would like to update only the Userdetail class and may be the FK might have changed. but the Employee object is not affected. only the child is now associated with different parent.
So i need to just update or insert the UserDetail with FK, but not to touch the Employee class. so i mentioned cascade to none,
but still I get the below error

Please help me on how to fix this


org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.general.model.Employee


Top
 Profile  
 
 Post subject: Re: many to one relation org.hibernate.TransientObjectException
PostPosted: Tue Jun 23, 2009 11:24 pm 
Newbie

Joined: Sun Mar 01, 2009 11:11 pm
Posts: 9
some one Please help me , in case if my previous post is too long to read or understand , i will summarize it



I have a dealer class , employee and user class, dealer can exist without employee and employee can exist without user. but the reverse is not possible.

Many employee can refer to one dealer and one user will refer to one employee .

creation of data will be in this order first dealer and then employee and last user.


When i try to create a user I get the above exception. I Don't want to manipulate the employee data while creating user, I just wanted to create/update the User and have the foreign key (employee id ) inserted/update.


Some one please guide me.

thanks in advance.


Top
 Profile  
 
 Post subject: Re: many to one relation org.hibernate.TransientObjectException
PostPosted: Wed Jun 24, 2009 12:27 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi kannanmugundan,

This is because of new EMP Detail is not in session. So what you can do associate Pk of your EMP Detail from screen and when is comes to transaction scope then fetch EMP detail Object by associated PK through same hibernate session and associate it with MyUserDetails. I hope that is gonna work.

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject: Re: many to one relation org.hibernate.TransientObjectException
PostPosted: Wed Jun 24, 2009 6:22 am 
Newbie

Joined: Sun Mar 01, 2009 11:11 pm
Posts: 9
Hi,

thanks for the reply, I just found there was some client code mistake, which i rectified and working now


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