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 default-lazy="false">
<class
name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
table="CCC.AGNT_MSG">
<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" lazy="false">
<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 default-lazy="false">
<class
name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
table="CCC.MSG_RECIPIENT">
<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" />
<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 for duplicating record/object:Code:
public void duplicate(Long messageId, String createUser) throws InfrastructureException {
logger.debug("duplicate: start: messageId: " + messageId);
logger.debug("duplicate: get session");
Session session = HibernateUtil.getSession();
logger.debug("duplicate: initial blank AgentMessageDVO");
AgentMessageDVO agentMessageDVO = null;
try {
logger.debug("duplicate: get AgentMessageDVO by id: " + messageId);
agentMessageDVO = (AgentMessageDVO) session.load(AgentMessageDVO.class, messageId);
logger.debug("duplicate: children: type: " + agentMessageDVO.getChildren().getClass() );
Vector xyz = (Vector)agentMessageDVO.getChildren();
logger.debug("duplicate: children: size: " + xyz.size() );
AgentMessageDVO newAgentMessageDVO = new AgentMessageDVO();
newAgentMessageDVO.setChildren( agentMessageDVO.getChildren() );
newAgentMessageDVO.setFromDate( agentMessageDVO.getFromDate() );
newAgentMessageDVO.setToDate( agentMessageDVO.getToDate() );
newAgentMessageDVO.setBehaviorCode( agentMessageDVO.getBehaviorCode() );
newAgentMessageDVO.setCategoryCode( agentMessageDVO.getCategoryCode() );
newAgentMessageDVO.setCreateUser( createUser );
newAgentMessageDVO.setCreateDate(new Date());
newAgentMessageDVO.setModUser( createUser );
newAgentMessageDVO.setModDate(new Date());
newAgentMessageDVO.setMessageText( agentMessageDVO.getMessageText() );
logger.debug("duplicate: call session update");
HibernateUtil.getSession().saveOrUpdate(newAgentMessageDVO);
} catch (HibernateException ex) {
logger.debug("duplicate: HibernateException: " + ex.getMessage() );
throw new InfrastructureException(ex);
} catch (Exception e) {
logger.debug("duplicate: Exception: " + e.getMessage() );
throw new InfrastructureException(e);
}
logger.debug("duplicate: finish");
}
Name and version of the database you are using: Oracle 9i
Ok, so I have a parent/child mapping. I can create the parent object, create the children, add them to the parent ... and when I save, I can save the parent and the children in one save. No problem there.
I want to be able to duplicate this object in the database. So, given an ID, I create a session and do a load(). The Parent object gets loaded, BUT the children DO NOT?!?!?!
I would presume that because of the mapping, when I load a parent, the children would come with it.
As it stands, I can duplicate the parent object just fine by creating a new instance of the parent, and then copying over the needed fields. So, if I got the children from the original parent, then I would be able to copy over that children to the new parent.
So, what is wrong with my mapping that only the parent is load()ing, but not the children.
Thanks for any help!