I am using Spring/hibernate 3.1. Trying a simple insert statement.
I have a method I am trying 1 to many using spring/hibernate/Oracle 9i/weblogic 8.1. Spring's LocalSessionFactoryBean is used for getting the session and weblogic jta is used. So Spring will manage session closing and transcation. Seems to a simple problem.
Step 1 : populating the parent object and invoking save method.
Step 2: Then calling the child (Set) by passing the parent. this is done because parent which is also a part of primary key for the child table (composite key). Adding a 2 childs in a set and assiging it to parent by calling the set method.
Step 3: Once again I am savingorUpdating the parent object.
Hibernate throws parent key not found.
Code between sessionFactory.openSession() and session.close():
public void createOrganization(long[] accountIds, List oAList,
String aowOrgName, Integer ultimateParentID, String aowAdminId) {
AowOrganization aowOrg = buildAowOrganization(aowOrgName,
ultimateParentID, aowAdminId);
Session aowSession = this.getSessionFactory().getCurrentSession();
aowSession.save(aowOrg);
/*aowSession.flush();
aowSession.refresh(aowOrg);
aowSession.evict(aowOrg);
aowSession.load(aowOrg, aowOrg.getOrgId());
*/
aowOrg.setOrgAccts(buildAccounts(aowOrg, accountIds, aowAdminId));
aowOrg.setOrgUsers(buildUsers(aowOrg, oAList, aowAdminId));
aowSession.saveOrUpdate(aowOrg);
//aowSession.refresh(aowOrg);
aowSession.flush();
}
private Set buildAccounts(AowOrganization aowOrg, long[] accountIds,
String aowAdminId) {
Set acctList = new HashSet(accountIds.length);
AowOrgAcct aowOrgAcct = null;
RasAcct rasAcct = null;
for (int i = 0; i < accountIds.length; i++) {
aowOrgAcct = new AowOrgAcct();
aowOrgAcct.setLastUptdBy(aowAdminId);
aowOrgAcct.setCrtnBy(aowAdminId);
aowOrgAcct.setActInd(AAConstants.ACCOUNT_ACTIVE_INDICATOR);
aowOrgAcct.setLastUptdDate(new Date());
rasAcct = new RasAcct();
aowOrgAcct.setRasAcct(getRasAccount(accountIds[i]));
aowOrgAcct.setAowOrganization(aowOrg);
this.getSessionFactory().getCurrentSession().save(aowOrgAcct);
acctList.add(aowOrgAcct);
}
return acctList;
}
private RasAcct getRasAccount(long accountId) throws HibernateException {
RasAcct rasAcct = new RasAcct();
Session aowSession = this.getSessionFactory().getCurrentSession();
aowSession.load(rasAcct, new Long(accountId));
return rasAcct;
}
private AowOrganization buildAowOrganization(String aowOrgName,
Integer ultimateParentID, String aowAdminId) {
AowOrganization aowOrganization = new AowOrganization();
aowOrganization.setOrgName(aowOrgName);
aowOrganization.setUltParentId(ultimateParentID);
aowOrganization.setCrtnBy(aowAdminId);
aowOrganization.setLastUptdBy(aowAdminId);
aowOrganization.setStatus(AAConstants.ORGANIZATION_ACTIVE_INDICATOR);
aowOrganization.setLastUptdDate(new Date());
return aowOrganization;
}
private Set buildUsers(AowOrganization aowOrg, List oAList,
String aowAdminId) {
Set usersList = new HashSet(oAList.size());
AowOrgUser aowOrgUser = null;
Iterator oAIterator = oAList.iterator();
while (oAIterator.hasNext()) {
aowOrgUser = (AowOrgUser) oAIterator.next();
aowOrgUser.setCrtnBy(aowAdminId);
aowOrgUser.setLastUptdBy(aowAdminId);
aowOrgUser.setStatus(AAConstants.USER_ACTIVE_INDICATOR);
/* Must be done by the UI Layer or Service Layer */
/*
* aowOrgUser.setType(new Short("1"));
* aowOrgUser.setUserId("xcos742");
*/
aowOrgUser.setLastUptdDate(new Date());
aowOrgUser.setAowOrganization(aowOrg);
this.getSessionFactory().getCurrentSession().save(aowOrgUser);
usersList.add(aowOrgUser);
}
return usersList;
}
Hibernate: insert into ARR_AOW_ORG (ORG_NAME, ULT_PAR_ID, ACT_IND, CRTD_BY,
LAST_UPTD_BY, CRTN_DT, LAST_UPTD_DATE, ORG_ID) values (?, ?, ?, ?, ?, ?, ?,
?)
Hibernate: insert into ARR_AOW_ORG_ACCT (ACT_IND, CRTN_DT, CRTD_BY,
LAST_UPTD_DATE, LAST_UPTD_BY, ORG_ID, ACCT_ID) values (?, ?, ?, ?, ?, ?, ?)
18:02:58,321 WARN JDBCExceptionReporter:71 - SQL Error: 2291, SQLState:
23000
18:02:58,321 ERROR JDBCExceptionReporter:72 - ORA-02291: integrity
constraint (ARR.ARR_AOW_ORG_ACCT_F1) violated - parent key not found
mapping file
In parent:
<set name="orgAccts" table="ARR_AOW_ORG_ACCT" inverse="true" cascade="all">
<key column="ORG_ID"/>
<one-to-many class="AowOrgAcct"/>
</set>
<set name="orgUsers" table="ARR_AOW_ORG_USER" inverse="true" cascade="all">
<key column="ORG_ID"/>
<one-to-many class="AowOrgUser"/>
</set>
In child 1:
<composite-id>
<key-many-to-one name="aowOrganization" column="ORG_ID" class="AowOrganization"/>
<key-many-to-one name="rasAcct" column="ACCT_ID" class="RasAcct"/>
</composite-id>
<many-to-one name="aowOrganization" class="AowOrganization" column="ORG_ID" insert="false" update="false" />
In child 2:
<composite-id>
<key-property name="userId" column="USER_ID" type="java.lang.String"/>
<key-many-to-one name="aowOrganization" column="ORG_ID" class="AowOrganization"/>
</composite-id>
<many-to-one name="aowOrganization" class="AowOrganization" column="ORG_ID" insert="false" update="false" />
Struggling to solve this and stuck here for a week. Seems to be a simple problem.....
|