Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping >
<class
name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
table="CCC.AGNT_MSG" lazy="true">
<id name="id" type="long">
<column name="MSG_ID" sql-type="NUMBER" />
<generator class="sequence">
<param name="sequence">CCC.SEQ_AGNT_MSG</param>
</generator>
</id>
<set name="recipients" cascade="all-delete-orphan" lazy="false" inverse="true" >
<key column="MSG_ID" />
<one-to-many class="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO" />
</set>
<property name="fromDate" type="date">
<column name="MSG_START_DATE" sql-type="DATE" not-null="false" />
</property>
<property name="toDate" type="date">
<column name="MSG_END_DATE" sql-type="DATE" not-null="false" />
</property>
<property name="messageText" type="string">
<column name="MSG_TEXT" sql-type="VARCHAR2(500)" not-null="false" />
</property>
<property name="createUser" type="string">
<column name="CREATE_USER_ID" sql-type="VARCHAR2(20)"
not-null="false" />
</property>
<property name="createDate" type="date">
<column name="CREATE_DATE" sql-type="DATE" not-null="false" />
</property>
<property name="modUser" type="string">
<column name="UPDATE_USER_ID" sql-type="VARCHAR2(20)"
not-null="false" />
</property>
<property name="modDate" type="date">
<column name="UPDATE_DATE" sql-type="DATE" not-null="false" />
</property>
<property name="categoryCode" type="string">
<column name="MSG_CATG_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>
<property name="behaviorCode" type="string">
<column name="MSG_BHVR_CD" sql-type="VARCHAR2(20)" not-null="false" />
</property>
<many-to-one name="messageCategoryDVO" column="MSG_CATG_CD" class="com.starwood.saratoga.dataaccess.dvo.MessageCategoryDVO" not-null="true" insert="false" update="false"/>
<many-to-one name="messageBehaviorDVO" column="MSG_BHVR_CD" class="com.starwood.saratoga.dataaccess.dvo.MessageBehaviorDVO" not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
table="CCC.MSG_RECIPIENT" lazy="false">
<composite-id>
<key-many-to-one name="myParent" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO">
<column name="MSG_ID"/>
</key-many-to-one>
<key-property name="recipientId" type="string" column="RECIPIENT_ID" />
</composite-id>
<property name="receiverCategoryCode" type="string">
<column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true" />
</property>
<many-to-one name="myParent" column="MSG_ID" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO" not-null="true" insert="false" update="false" cascade="all" fetch="select" lazy="true" />
<many-to-one name="recvCatgCd" column="RCVR_CATG_CD" class="com.starwood.saratoga.dataaccess.dvo.ReceiverCategoryDVO" not-null="true" insert="false" update="false"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public void makePersistent(AgentMessageDVO agentMessageDVO) throws InfrastructureException {
logger.debug("makePersistent: start");
try {
logger.debug("makePersistent: saveOrUpdate: start");
HibernateUtil.getSession().saveOrUpdate(agentMessageDVO);
logger.debug("makePersistent: saveOrUpdate: finish");
} catch (HibernateException ex) {
logger.debug("makePersistent: HibernateException: " + ex.getMessage() );
throw new InfrastructureException(ex);
} catch (Exception e) {
logger.debug("makePersistent: Exception: " + e.getMessage() );
throw new InfrastructureException(e);
}
logger.debug("makePersistent: finish");
}
Full stack trace of any exception that occurs: N/A
Name and version of the database you are using: oracle 9i
The generated SQL (show_sql=true):Code:
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.MSG_RECIPIENT where MSG_ID=? and RECIPIENT_ID=?
delete from CCC.AGNT_MSG where MSG_ID=?
Debug level Hibernate log excerpt: N/A
I want to update records in my database. So, if I don't touch any children records, I can modify the parent fields with no problems.
But, because of the way my app is ... the children may change all the time. So, I presumed it would be easier to remove them all, and just add the new children.
I tried to just delete the old set and add a new set, and that didn't work. When I used parent.getChildren().clear(); and then did a session.flush. Not only did it get rid of the children, but the parent as well. No! I want to keep the parent and delete the children.
I only want to delete the children IF AND ONLY IF I delete the parent. But, if I delete all the children, then I want to keep the parent, and then add new children.
So, I tried and iterative process to delete one child at a time, and since I deleted all the children ... it removed the parent as well.
Please ... any help would be most appreciated!