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