Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: 3.0.5
Mapping documents:
Parent Table
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>
<set name="readMessages" cascade="all-delete-orphan" lazy="false" inverse="true" >
<key column="MSG_ID" />
<one-to-many class="com.starwood.saratoga.dataaccess.dvo.ReceivedMessageDVO" />
</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>
Child Table One 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" />
<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>
Child Table Two 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.ReceivedMessageDVO"
table="CCC.RCVD_MSG">
<composite-id>
<key-many-to-one name="parentMessage" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO">
<column name="MSG_ID"/>
</key-many-to-one>
<key-many-to-one name="parentUser" class="com.starwood.saratoga.dataaccess.dvo.SaratogaUserDVO">
<column name="LDAP_ID"/>
</key-many-to-one>
</composite-id>
<property name="readDate" type="date">
<column name="MSG_READ_DTME" sql-type="DATE" not-null="false" />
</property>
<property name="receivedStatus" type="long">
<column name="RCVD_MSG_STS_ID" sql-type="NUMBER" not-null="false" />
</property>
<many-to-one name="receivedStatusDVO" column="RCVD_MSG_STS_ID" class="com.starwood.saratoga.dataaccess.dvo.ReceivedStatusDVO" not-null="true" insert="false" update="false"/>
<many-to-one name="parentMessage" column="MSG_ID" class="com.starwood.saratoga.dataaccess.dvo.AgentMessageDVO" not-null="true" insert="false" update="false" />
<many-to-one name="parentUser" column="LDAP_ID" class="com.starwood.saratoga.dataaccess.dvo.SaratogaUserDVO" not-null="true" insert="false" update="false" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
public Collection getMessagesByUser(String ldapId) throws InfrastructureException {
logger.debug("getMessagesByUser: start: ldapId=" + ldapId);
String sql = "select DISTINCT {m.*},{r.*},{rd.*} " +
" from AGNT_MSG {m} INNER JOIN MSG_RECIPIENT {r} ON {m}.MSG_ID = {r}.MSG_ID " +
" LEFT OUTER JOIN RCVD_MSG {rd} ON {m}.MSG_ID = {rd}.MSG_ID " +
" WHERE ( {r}.RECIPIENT_ID = :recipientId ) ";
Collection messages = null;
try {
logger.debug("getMessagesByUser: get messages: ldapId=" + ldapId);
Session session = HibernateUtil.getSession();
Query qry = session.createSQLQuery(sql)
.addEntity("m",AgentMessageDVO.class)
.addEntity("r",MessageRecipientDVO.class)
.addEntity("rd",ReceivedMessageDVO.class)
.setParameter("recipientId",ldapId);
messages = qry.list();
logger.debug("getMessagesByUser: get messages: size: " + messages.size() );
} catch (HibernateException ex) {
logger.debug("getMessagesByUser: HibernateException: " + ex.getMessage());
throw new InfrastructureException(ex);
}
logger.debug("getMessagesByUser: finish: return messages: size: " + messages.size() );
return messages;
}
Full stack trace of any exception that occurs: N/A at this time
Name and version of the database you are using: Oracle 9i
The generated SQL (show_sql=true):Code:
[8/10/05 10:45:06:074 EDT] 1b3d475f SystemOut O Hibernate: select DISTINCT m.MSG_ID as MSG1_0_, m.MSG_START_DATE as MSG2_0_0_, m.MSG_END_DATE as MSG3_0_0_, m.MSG_TEXT as MSG4_0_0_, m.CREATE_USER_ID as CREATE5_0_0_, m.CREATE_DATE as CREATE6_0_0_, m.UPDATE_USER_ID as UPDATE7_0_0_, m.UPDATE_DATE as UPDATE8_0_0_, m.MSG_CATG_CD as MSG9_0_0_, m.MSG_BHVR_CD as MSG10_0_0_ from AGNT_MSG m INNER JOIN MSG_RECIPIENT r ON m.MSG_ID = r.MSG_ID WHERE ( (r.RECIPIENT_ID LIKE ?) )
[8/10/05 10:45:06:094 EDT] 1b3d475f SystemOut O Hibernate: select readmessag0_.MSG_ID as MSG1_1_, readmessag0_.LDAP_ID as LDAP2_1_, readmessag0_.MSG_ID as MSG1_0_, readmessag0_.LDAP_ID as LDAP2_0_, readmessag0_.MSG_READ_DTME as MSG3_7_0_, readmessag0_.RCVD_MSG_STS_ID as RCVD4_7_0_, readmessag0_.MSG_ID as MSG1_7_0_, readmessag0_.LDAP_ID as LDAP2_7_0_ from CCC.RCVD_MSG readmessag0_ where readmessag0_.MSG_ID=?
[8/10/05 10:45:06:094 EDT] 1b3d475f SystemOut O Hibernate: select recipients0_.MSG_ID as MSG1_1_, recipients0_.RECIPIENT_ID as RECIPIENT2_1_, recipients0_.MSG_ID as MSG1_0_, recipients0_.RECIPIENT_ID as RECIPIENT2_0_, recipients0_.RCVR_CATG_CD as RCVR3_1_0_, recipients0_.MSG_ID as MSG1_1_0_ from CCC.MSG_RECIPIENT recipients0_ where recipients0_.MSG_ID=?
[8/10/05 10:45:06:104 EDT] 1b3d475f SystemOut O Hibernate: select readmessag0_.MSG_ID as MSG1_1_, readmessag0_.LDAP_ID as LDAP2_1_, readmessag0_.MSG_ID as MSG1_0_, readmessag0_.LDAP_ID as LDAP2_0_, readmessag0_.MSG_READ_DTME as MSG3_7_0_, readmessag0_.RCVD_MSG_STS_ID as RCVD4_7_0_, readmessag0_.MSG_ID as MSG1_7_0_, readmessag0_.LDAP_ID as LDAP2_7_0_ from CCC.RCVD_MSG readmessag0_ where readmessag0_.MSG_ID=?
[8/10/05 10:45:06:104 EDT] 1b3d475f SystemOut O 1898602 [Servlet.Engine.Transports : 0] DEBUG com.starwood.saratoga.dataaccess.dao.AgentMessageDAO - findByCriteria: Exception: null
[8/10/05 10:45:06:114 EDT] 1b3d475f RequestProces W org.apache.struts.action.RequestProcessor Unhandled Exception thrown: class com.starwood.saratoga.dataaccess.InfrastructureException
[8/10/05 10:45:06:124 EDT] 1b3d475f LocalTranCoor E WLTC0033E: Resource jdbc/OraDb rolled back in cleanup of unresolved LocalTransactionContainment.
Debug level Hibernate log excerpt: N/A at this time
Ok, I did have a parent table with one child table. The parent was for messages, and the child was for recipients. I had the mapping all set and my application could CRUD very nicely.
Now, they have added a new table. They created a composite-primary-key, lets say MSG_ID and LDAP_ID
This new table is like a join table since MSG_ID is a FK back to the messages parent table and LDAP_ID is a FK back to another parent table, but I am not so much worried about that table.
So, what we have here: M = parent table
/ \
child table 1 child table 2
So, here are my questions:
Can I just write a parent record to M and records to child table 1 WITHOUT writing to child table 2?
If I have no records in child table 2, but do have records in child table1, and I going to have any problems?
As soon as a I modified my parent hbm.xml file to add this new child, and then I tried getting messages like I was (knowing that there were no new records in child table 2. I started getting NullPointerExceptions in my hashcode in the new child2 table pojo. I'll provide that code also:
Code:
public class ReceivedMessageDVO extends BaseDVO {
private long messageId;
private AgentMessageDVO parentMessage;
private String ldapId;
private SaratogaUserDVO parentUser;
private Date readDate;
private long receivedStatus;
private ReceivedStatusDVO receivedStatusDVO;
// ******************************************* //
public ReceivedMessageDVO(){
}
public ReceivedMessageDVO( AgentMessageDVO parentMessage, SaratogaUserDVO parentUser, Date readDate, ReceivedStatusDVO receivedStatusDVO ) {
this.parentMessage = parentMessage;
this.messageId = parentMessage.getId();
this.parentUser = parentUser;
this.ldapId = parentUser.getLdapId();
this.readDate = readDate;
this.receivedStatusDVO = receivedStatusDVO;
this.receivedStatus = receivedStatusDVO.getCode();
}
// ******************************************* //
/**
* @return Returns the messageId.
*/
public long getMessageId() {
return messageId;
}
/**
* @param messageId The messageId to set.
*/
public void setMessageId(long Id) {
this.messageId = Id;
}
// ******************************************* //
/**
* @return Returns the messageId.
*/
public AgentMessageDVO getParentMessage() {
return parentMessage;
}
/**
* @param messageId The messageId to set.
*/
public void setParentMessage(AgentMessageDVO message) {
this.parentMessage = message;
}
// ******************************************* //
/**
* @return Returns the ldapId.
*/
public String getLdapId() {
return ldapId;
}
/**
* @param ldapId The ldapId to set.
*/
public void setLdapId(String Id) {
this.ldapId = Id;
}
/**
* @return Returns the parentUser.
*/
public SaratogaUserDVO getParentUser() {
return parentUser;
}
/**
* @param parentUser The parentUser to set.
*/
public void setParentUser(SaratogaUserDVO user) {
this.parentUser = user;
}
// ******************************************* //
/**
* @return Returns the readDate.
*/
public Date getReadDate() {
return readDate;
}
/**
* @param readDate The readDate to set.
*/
public void setReadDate(Date readDate) {
this.readDate = readDate;
}
// ******************************************* //
//private long receivedStatus;
//private ReceivedStatusDVO receivedStatusDVO;
/**
* @return Returns the receivedStatus.
*/
public long getReceivedStatus() {
return receivedStatus;
}
/**
* @param receivedStatus The receivedStatus to set.
*/
public void setReceivedStatus(long receivedStatus) {
this.receivedStatus = receivedStatus;
}
/**
* @return Returns the receivedStatusDVO
*/
public ReceivedStatusDVO getReceivedStatusDVO() {
return receivedStatusDVO;
}
/**
* @param dvo The receivedStatusDVO to set.
*/
public void setReceivedStatusDVO(ReceivedStatusDVO dvo) {
this.receivedStatusDVO = dvo;
}
// ******************************************* //
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof ReceivedMessageDVO)) return false;
final ReceivedMessageDVO receivedMessageDVO = (ReceivedMessageDVO) o;
if (messageId != receivedMessageDVO.messageId) return false;
if (!ldapId.equals(receivedMessageDVO.ldapId)) return false;
return true;
}
public int hashCode() {
int result;
int id = (int)messageId;
result = 29 * id;
result = 29 * result + ldapId.hashCode();
return result;
}
}
Any help would be much appreciated!