Hi,
i have a problem while saving records,
I have a parent , a child (one-many relationship). I'm trying to set the values in my test class and save the record.
I'm using JPA ORM Mapping and the mapping for this is .
<entity class="UserPreference" name="UserPreference">
<table name="USR_PRFR" />
<sequence-generator name="UserPreferenceSeq" sequence-name="USR_PRFR_SEQ" />
<attributes>
<id name="id">
<column name="USR_PRFR_ID" />
<generated-value strategy="SEQUENCE" generator="UserPreferenceSeq" />
</id>
<basic name="userId">
<column name="USR_ID"/>
</basic>
<one-to-many name="userPrfrDetails"
target-entity="UserPreferenceDetail" fetch="EAGER">
<join-column name="USR_PRFR_ID" referenced-column-name="USR_PRFR_ID" nullable="false" />
<cascade>
<cascade-merge/>
</cascade>
</one-to-many>
<transient name="dataObjectIDClass" />
</attributes>
</entity>
<entity class="UserPreferenceDetail" name="UserPreferenceDetail">
<table name="USR_PRFR_DTL" />
<sequence-generator name="UserPrefDetSeq" sequence-name="USR_PRFR_DTL_SEQ"></sequence-generator>
<attributes>
<id name="id">
<column name="USR_PRFR_DTL_ID"/>
<generated-value strategy="SEQUENCE" generator="UserPrefDetSeq"/>
</id>
<basic name="name">
<column name="NM"/>
</basic>
<basic name="displayName">
<column name="DISP_NM"/>
</basic>
<basic name="value">
<column name="VAL"/>
</basic>
<many-to-one name="type" target-entity="com.pge.gtnd.cgts.dao.lookup.Lookup">
<join-column name="TYP_ID" referenced-column-name="LKUP_ID" />
<cascade>
<cascade-merge/>
</cascade>
</many-to-one>
<transient name="dataObjectIDClass" />
</attributes>
</entity>
User Preference is the Master table and User Preference Detail is the child table.
The test data is
public void testSaveUserPreferences() throws Exception{
IUserPreferenceDAO userPrefDAO = (IUserPreferenceDAO)getBean("userPrefDAO");
IUserPreference<?> userPreference = new UserPreference();
ILookupDAO lkupDao = (ILookupDAO) getBean(LOOKUP_DAO_BEAN);
userPreference.setUserId(13258);
userPreference.setAuditUserName(new AuditUserName("test"));
IUserPreferenceDetail<?> userPrefDetail = new UserPreferenceDetail();
userPrefDetail.setDisplayName("DisplayName");
userPrefDetail.setName("Name");
ILookup<?> type = lkupDao.getLookup(new LookupID(11801));
userPrefDetail.setType(type);
userPrefDetail.setValue("value");
HashSet<IUserPreferenceDetail<?>> usrPrfDtl = new HashSet<IUserPreferenceDetail<?>>();
usrPrfDtl.add(userPrefDetail);
userPreference.setUserPrfrDetails(usrPrfDtl);
userPrefDAO.saveUserPreference(userPreference);
}
and the save is
public void saveUserPreference(IUserPreference userPreference) throws Exception {
EntityManager em = getEntityManager();
em.merge(userPreference);
}
the problem is while trying to save , I get an exception saying it's a Stale Object.
Hibernate: select USR_PRFR_SEQ.nextval from dual
Hibernate: select lookup0_.LKUP_ID as LKUP1_51_1_, lookup0_.CRT_DT as CRT2_51_1_, lookup0_.CRT_USR as CRT3_51_1_, lookup0_.UPD_DT as UPD4_51_1_, lookup0_.UPD_USR as UPD5_51_1_, lookup0_.DSC as DSC51_1_, lookup0_.LKUP_TYP_ID as LKUP10_51_1_, lookup0_.SEQ_NUM as SEQ7_51_1_, lookup0_.SHRT_CUT_KEY as SHRT8_51_1_, lookup0_.SHRT_NM as SHRT9_51_1_, lookuptype1_.LKUP_TYP_ID as LKUP1_52_0_, lookuptype1_.CRT_DT as CRT2_52_0_, lookuptype1_.CRT_USR as CRT3_52_0_, lookuptype1_.UPD_DT as UPD4_52_0_, lookuptype1_.UPD_USR as UPD5_52_0_, lookuptype1_.DSC as DSC52_0_, lookuptype1_.EDT_IND as EDT7_52_0_, lookuptype1_.TYP as TYP52_0_ from gts3.LKUP lookup0_ left outer join gts3.lkup_typ lookuptype1_ on lookup0_.LKUP_TYP_ID=lookuptype1_.LKUP_TYP_ID where lookup0_.LKUP_ID=?
Hibernate: select USR_PRFR_DTL_SEQ.nextval from dual
Hibernate: insert into gts3.USR_PRFR (CRT_DT, CRT_USR, UPD_DT, UPD_USR, USR_ID, USR_PRFR_ID) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into gts3.USR_PRFR_DTL (CRT_DT, CRT_USR, UPD_DT, UPD_USR, DISP_NM, NM, TYP_ID, VAL, USR_PRFR_ID, USR_PRFR_DTL_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: update gts3.USR_PRFR set CRT_DT=?, CRT_USR=?, UPD_DT=?, UPD_USR=?, USR_ID=? where USR_PRFR_ID=? and UPD_DT=?
[ERROR] 12/11/2007-16:39:19 | event.def.AbstractFlushingEventListener | Could not synchronize database state with session
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [com.pge.gtnd.cgts.dao.nomination.UserPreference#189500]
Any help is highly appreciated, I'm a newbie.
|