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">
<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="save-update">
<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">
<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>
Name and version of the database you are using: Oracle 9i
The mapping works fine, and I have matching POJO's. I can put data into the Oracle database with no problems.
So, here I have a parent/child relationship. Some data for the record is in the parent and the other data is in the child table. With the query code I do get some records, and all is well there. I can see I am getting a Collection of records.
The question is ... what exactly am I getting back? A JDBC Query recordset? A collection of "AgentMessageDVO" classes? A hashmap? When I look at my collection that I got back, I can iterate through my records and each one says that it is a java.lang.Object ... but which object?
I'd like to iterate through each object in the collection and cast it as something else, but if I don't know what is in the list, then I can't cast it correctly or I get a classcastexception.
Any help would be much appreciated.
Code:
public Collection findByCriteria(HashMap criteria) throws InfrastructureException {
logger.debug("findByCriteria: start");
String sql = getMessageCriteria(criteria);
sql = "select {m.*},{r.*} from AGNT_MSG {m}, MSG_RECIPIENT {r} WHERE {m}.MSG_ID = {r}.MSG_ID " +
" AND ( ({m}.MSG_START_DATE >= TO_DATE(:fromDate,'mm/dd/yy')) " +
" AND ({m}.MSG_END_DATE <= TO_DATE(:toDate,'mm/dd/yy')) ) " +
" AND ( ({r}.RECIPIENT_ID LIKE :recipientId ) OR ({r}.RECIPIENT_ID LIKE :roleSelected ) ) ";
Collection messages;
try {
logger.debug("findByCriteria: get messages");
Session session = HibernateUtil.getSession();
Query qry = session.createSQLQuery(sql)
.addEntity("m",AgentMessageDVO.class)
.addEntity("r",MessageRecipientDVO.class)
.setParameter("fromDate","07/01/2005")
.setParameter("toDate","07/10/2005")
.setParameter("recipientId","%wpsadmin%")
.setParameter("roleSelected","%CCC_ADMIN%");
messages = qry.list();
} catch (HibernateException ex) {
logger.debug("findByCriteria: HibernateException: " + ex.getMessage());
throw new InfrastructureException(ex);
} catch (Exception e) {
logger.debug("findByCriteria: Exception: " + e.getMessage());
throw new InfrastructureException(e);
}
logger.debug("findByCriteria: finish: return messages");
return messages;
}