aspose file tools*
The moose likes Object Relational Mapping and the fly likes Error retrieving child objects Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Error retrieving child objects" Watch "Error retrieving child objects" New topic
Author

Error retrieving child objects

Seema Sajnani
Greenhorn

Joined: Sep 14, 2011
Posts: 8
Here is the table structure we have

DSPS_TPC_TYP

DSPS_TPC_TYP_ID smallInt PK
DSPS_TPC_TYP_DSC char(100)
DSPS_PRVG_TYP_CDE smallInt


DSPS_SBTPC_TYP

DSPS_SBTPC_TYP_ID smallInt PK
DSPS_SBTPC_TYP_DSC char(100)
DSPS_TPC_TYP_ID FK from DSPS_TPC_TYP table

there can be multiple records in DSPS_SBTPC_TYP for a DSPS_TPC_TYP

This is how i have my DTO objects defined


public class TopicDTO implements java.io.Serializable {


// Fields

private long topicId;
private String topicDescription;
private long topicType;
private Set<SubTopicDTO> subTopics;
getter/setters

}

public class SubTopicDTO implements java.io.Serializable {


// Fields

private Long subTopicId;
private String subTopicDescription;
private String active = "Y";
private Long rlseNum;
private TopicDTO topic;

}

mapping files
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.callDisposition.dto.TopicDTO" table="DSPS_TPC_TYP" lazy="false">
<id name="topicId" type="java.lang.Long">
<column name="DSPS_TPC_TYP_ID" />
</id>
<property name="topicDescription" type="java.lang.String">
<column name="DSPS_TPC_TYP_DSC" length="100" />
</property>
<property name="topicType" type="java.lang.Long" >
<column name="DSPS_PRVG_TYP_CDE" length="6" not-null="true" />
</property>
<set name="subTopics" table="DSPS_SBTPC_TPY" inverse="true" lazy="true" fetch="select">
<key>
<column name="DSPS_TPC_TPY_ID" not-null="true" />
</key>
<one-to-many class="com.callDisposition.dto.SubTopicDTO" />
</set>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.callDisposition.dto.SubTopicDTO" table="DSPS_SBTPC_TYP" lazy="false">
<id name="subTopicId" type="java.lang.Long">
<column name="DSPS_SBTPC_TYP_ID" />
</id>
<property name="subTopicDescription" type="java.lang.String">
<column name="DSPS_SBTPC_TYP_DSC" length="100" />
</property>
<many-to-one name="topic" class="com.callDisposition.dto.TopicDTO" fetch="select"> <column name="DSPS_TPC_TYP_ID" not-null="true"></column> </many-to-one>
</class>
</hibernate-mapping>

DAO Class
public CallDispositionVO getTopicsForType(Long type) throws Exception {
String METHOD_NAME = "getTopicsForType";
CallDispositionVO callVO = new CallDispositionVO();
LOG.debug(LOG_AREA+ "methodName = "+METHOD_NAME+" | Entry");
try{

LOG.debug(LOG_AREA+ "methodName = "+METHOD_NAME+" | Initializing the transaction");
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();

LOG.debug(LOG_AREA+ "methodName = "+METHOD_NAME+" | Calling the query");
String SQL_QUERY = "from TopicDTO where topicType = "+type.intValue();
Query resultSet = session.createQuery(SQL_QUERY);


List resultList = resultSet.list();

if (resultList != null && resultList.size() > 0) {
for (Iterator iterator = resultList.iterator(); iterator.hasNext();){
TopicDTO topic = (TopicDTO) iterator.next();
System.out.print("Topic: " + topic.getTopicDescription());
System.out.print(" SubTopics: " + topic.getSubTopics().size());
for (Iterator iterator2 = topic.getSubTopics().iterator(); iterator2.hasNext();){
SubTopicDTO subTopic = (SubTopicDTO) iterator2.next();
System.out.print(" SubTopics " + subTopic.getSubTopicDescription());
}

}
}

tx.commit();
session.close();
LOG.debug(LOG_AREA+ "methodName = "+METHOD_NAME+" | Session closed");
}catch(Exception e){
LOG.error(LOG_AREA+ "methodName = "+METHOD_NAME+" | Exception : "+e);
throw e;
}
LOG.debug(LOG_AREA+ "methodName = "+METHOD_NAME+" | Exit");
return callVO;
}

The result has the topics populated with subtopics field null and when I call getSubtopics() i get the following exception

org.hibernate.exception.SQLGrammarException: could not initialize a collection: [com.callDisposition.dto.TopicDTO.subTopics#8]
Error: [IBM][CLI Driver][DB2] SQL0206N "SUBTOPICS0_.DSPS_TPC_TPY_ID" is not valid in the context where it is used. SQLSTATE=42703

this is the sql generated
select subtopics0_.DSPS_TPC_TPY_ID as DSPS11_1_, subtopics0_.DSPS_SBTPC_TYP_ID as DSPS1_1_, subtopics0_.DSPS_SBTPC_TYP_ID as DSPS1_55_0_, subtopics0_.DSPS_SBTPC_TYP_DSC as DSPS2_55_0_, subtopics0_.DSPS_TPC_TYP_ID as DSPS3_55_0_ from CRS3DEVL.DSPS_SBTPC_TYP subtopics0_ where subtopics0_.DSPS_TPC_TPY_ID=?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Error retrieving child objects
 
Similar Threads
Hibernate .list() throwing SQLGrammarException
Many to one relationshilp table have same entity name returns wrong value
select in hibernate
Problems with using property in formula [hibernate]
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]