Hibernate version: 3.1.2
Name and version of the database you are using: Sybase ASE 12.5.3
ISSUE:
We are using an interceptor extended from the EmptyInterceptor. When saving the AgreementAmendment, we would like to add an amendment detail record. In the onFlushDirty, I have tried to update the object entity passed in, but nothing happens. When I update an entry in the currentState object array, it works, but only if the object is a single entity such as a String, DateTime, etc. When I try to add an item into the details "set", nothing happens. I do this by getting the Set out of the specific currentState object, adding a detail record to the Set, and then resetting the currentState object to the "updated" set... Please advise with any suggestions or changes to what I am doing.. Thanks...
Unittest code is below, followed by the interceptor code. Lastly, I have listed the hibernate mappings for the two objects..:
Unit test sample
Code:
public void testSave() throws Exception {
AgreementAmendmentHibernateDao dao = (AgreementAmendmentHibernateDao) applicationContext.getBean(
"agreementAmendmentDao", AgreementAmendmentHibernateDao.class);
AgreementAmendment amendment = dao.findById(TEST_TYPE_INT_KEY_EXISTING);
boolean ofdiExecInd = false;
if (amendment.isOfdiExecInd()){
amendment.setOfdiExecInd(false);
}else{
amendment.setOfdiExecInd(true);
ofdiExecInd = true;
}
AgreementAmendment savedAmendment = null;
amendment.setUpdateDocId(TEST_DOC_ID);
Timestamp now = new Timestamp(System.currentTimeMillis());
amendment.setUpdateDate(now);
amendment.setUpdateUserId(TEST_USER_ID);
savedAmendment = dao.saveOrUpdate(amendment);
dao.flush();
if (ofdiExecInd){
assertEquals(savedAmendment.isOfdiExecInd(), true);
}else{
assertEquals(savedAmendment.isOfdiExecInd(), false);
}
}
Interceptor Sample Code:
public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState, Object[] previousState,
String[] propertyNames, Type[] types) throws CallbackException {
if ( entity instanceof AgreementAmendment ){
AgreementAmendment amendment = (AgreementAmendment)entity;
for (int ii = 0; ii < propertyNames.length; ii++) {
if (propertyNames[ii].equals("details")){
Set<AgreementAmendmentDetail> details = (Set<AgreementAmendmentDetail>)currentState[ii];
AgreementAmendmentDetail amendmentDetail = new AgreementAmendmentDetail();
amendmentDetail.setAgreementAmendment(amendment);
amendmentDetail.setAmendmentDetailType(AmendmentDetailType.getOther());
amendmentDetail.setUpdateDate(amendment.getUpdateDate());
amendmentDetail.setUpdateUserId(amendment.getUpdateUserId());
details.add(amendmentDetail);
// this does not work....
currentState[ii] = details;
}else if (propertyNames[ii].equals("amendmentComment")){
// this works...
currentState[ii] = "Test comment";
}
}
return true;
}
}
Mapping documents:Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.oppen.alliance.agreement">
<!-- Define User Types -->
<class name="AgreementAmendment" table="agreement_amendment_v1">
<comment>Agreements specific to servicing firms.</comment>
<id name="amendmentId" column="amendment_id" type="int">
<generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
<param name="table">id_value_v1</param>
<param name="primary_key_column">table_name</param>
<param name="primary_key_value">agreement_amendment</param>
<param name="value_column">next_hi_multiplier</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="amendmentDocId" column="amendment_doc_id" type="string" />
<property name="amendmentNumber" column="amendment_number" type="int" />
<property name="ofsExecInd" column="ofs_exec_ind" type="boolean" />
<property name="ofiExecInd" column="ofi_exec_ind" type="boolean" />
<property name="ofdiExecInd" column="ofdi_exec_ind" type="boolean" />
<property name="amendmentComment" column="amendment_comment" type="string" />
<property name="amendmentDate" column="amendment_date" type="timestamp" />
<property name="amendmentReasonType" column="amendment_reason_type" lazy="false">
<type name="com.oppen.ec.hibernate.CodeUserType">
<param name="className">com.oppen.alliance.code.AmendmentReasonType</param>
</type>
</property>
<many-to-one name="servicingAgreement" class="com.oppen.alliance.agreement.servicing.ServicingAgreement"
column="servicing_agreement_id" not-null="true" />
<set name="details" cascade="save-update" inverse="true">
<key column="amendment_id" />
<one-to-many class="AgreementAmendmentDetail" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.oppen.alliance.agreement">
<!-- Define User Types -->
<class name="AgreementAmendmentDetail" table="agreement_amendment_detail_v1">
<comment>Agreement Amendment Detail Mapping</comment>
<id name="amendmentDetailId" column="amendment_detail_id" type="int">
<generator class="org.hibernate.id.MultipleHiLoPerTableGenerator">
<param name="table">id_value_v1</param>
<param name="primary_key_column">table_name</param>
<param name="primary_key_value">agreement_amendment_detail</param>
<param name="value_column">next_hi_multiplier</param>
<param name="max_lo">100</param>
</generator>
</id>
<property name="auditId" column="audit_id" type="int" />
<property name="updateDate" column="update_date" type="timestamp" />
<property name="updateUserId" column="update_user_id" type="string" />
<property name="amendmentDetailType" column="amendment_detail_type" lazy="false">
<type name="com.oppen.ec.hibernate.CodeUserType">
<param name="className">com.oppen.alliance.code.AmendmentDetailType</param>
</type>
</property>
<many-to-one name="agreementAmendment" class="com.oppen.alliance.agreement.AgreementAmendment"
column="amendment_id" not-null="true" />
</class>
</hibernate-mapping>