Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 2.0.x
Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class
name="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO"
table="CCC.AGNT_MSG">
<id name="id" type="long" unsaved-value="0">
<column name="MSG_ID" sql-type="NUMBER" not-null="true" />
<generator class="sequence">
<param name="sequence">CCC.SEQ_AGNT_MSG</param>
</generator>
</id>
<set name="recipients" cascade="save-update"table="MSG_RECIPIENT">
<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>
<class name="com.starwood.saratoga.dataaccess.dvo.MessageRecipientDVO"
table="CCC.MSG_RECIPIENT">
<composite-id name="compositeKey" class="com.starwood.saratoga.dataaccess.dao.MessageRecipientCompositeKey">
<key-property name="messageId" type="long" column="MSG_ID"/>
<key-property name="recipientId" type="string" column="RECIPIENT_ID"/>
<key-property name="receiverCategoryCode" type="string" column="RCVR_CATG_CD"/>
</composite-id>
<property name="recipientId" type="string" insert="false" update="false" >
<column name="RECIPIENT_ID" sql-type="VARCHAR2(50)" not-null="true"/>
</property>
<property name="receiverCategoryCode" type="string" insert="false" update="false" >
<column name="RCVR_CATG_CD" sql-type="VARCHAR2(20)" not-null="true"/>
</property>
<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:
Session session = null;
try {
System.out.println("AgentMessageDAO: create Transaction");
Transaction tx = session.beginTransaction();
System.out.println("AgentMessageDAO: create session");
session = HibernateUtil.currentSession();
System.out.println("AgentMessageDAO: saveOrUpdate: start");
session.saveOrUpdate(agentMessageDVO);
System.out.println("AgentMessageDAO: saveOrUpdate: finish: start tx commit");
tx.commit();
System.out.println("AgentMessageDAO: finish tx commit");
} catch (HibernateException e) {
log("AgentMessageDVO: saveOrUpdate: Hibernate Error Occured");
e.printStackTrace();
} finally {
try {
HibernateUtil.closeSession(session);
} catch (Exception ignore) {
log("AgentMessageDVO: saveOrUpdate: Session Close Error Occured");
ignore.printStackTrace();
}
}
Full stack trace of any exception that occurs: N/A now
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true): N/A for now
Debug level Hibernate log excerpt: N/A for now
Composite Key CodeCode:
public class MessageRecipientCompositeKey extends CompositeKey {
private long messageId;
private String recipientId;
private String receiverCategoryCode;
public int hashCode() {
int tmp = 0;
tmp = (messageId + recipientId + receiverCategoryCode).hashCode();
return tmp;
}
/**
* @return Returns the messageId.
*/
public long getMessageId() {
return messageId;
}
/**
* @param ldapID The ldapID to set.
*/
public void setMessageId(long messageId) {
this.messageId = messageId;
}
/**
* @return Returns the recipientId.
*/
public String getRecipientId() {
return recipientId;
}
/**
* @param recipientId The recipientId to set.
*/
public void setRecipientId(String recipientId) {
this.recipientId = recipientId;
}
/**
* @return Returns the receiverCategoryCode.
*/
public String getReceiverCategoryCode() {
return receiverCategoryCode;
}
/**
* @param receiverCategoryCode The receiverCategoryCode to set.
*/
public void setReceiverCategoryCode(String receiverCategoryCode) {
this.receiverCategoryCode = receiverCategoryCode;
}
}
I apologize for asking about this question since I am a Hibernate newbie, and I just bought Hibernate in Action only moments ago, and I will start reading it.
I know this question has been asked a few times, so I figured it couldn't hurt to ask again.
From the data provided, I have a table and by itself I can add data to it just fine. Now, I have a child table, and this will be a one-to-many relationship. A MSG_ID is the PK of the parent table set with a sequence. The child table has a composite key of 3 fields ... one of which is MSG_ID which should come from the parent table.
So, is this possible to do?
Is there a special mapping I can use?
Do I need to create a special composite key class?
Can I get the MSG_ID after the first table is written and use another
saveOrUpdate to write my child table in a transaction?
Which is the best preferred method?
Thanks for any help and sorry to be a pain!
Tom