Dear experts,
I am facing problem in composite-element mapping of hibernate. My expected relation is many to many in document and task. That is a task may have multiple documents and a document may have assigned to multiple task. Relational table will contain both foreign keys with additional column.
My Task hbm file:
Code:
<class name="com.dmcs.model.tm.TaskInfo" table="TM_TASK">
<id name="taskId" column="TASK_ID">
<generator class="increment" />
</id>
<property name="taskUdId" column="TASK_UDID" />
<set name="docInfoList" table="TM_TASKDOCS" cascade="save-update">
<key column="taskId" />
<composite-element class="com.dmcs.model.tm.TaskDocsInfo">
<many-to-one name="docInfo" column="documentId" class="com.dmcs.model.document.DocumentInfo" />
<property name="rmks" column="RMKS"/>
</composite-element>
</set>
</class>
My document hbm file:
Code:
<class name="com.dmcs.model.document.DocumentInfo" table="DOCUMENT">
<id name="documentId" column="DOC_ID">
<generator class="increment" />
</id>
<property name="fileName" column="FILENAME" />
<set name="taskInfoList" table="TM_TASKDOCS" cascade="save-update">
<key column="documentId" />
<many-to-many column="taskId" class="com.dmcs.model.tm.TaskInfo" />
</set>
</class>
Suppose I want to create a new task T1 (not already save in database) with assigned three Documents(D1,D2,D3 ;assume D1, D2, D3 already saved in database). what will be the TaskDocsInfo class code exactly? I have tried with the following code but fails
Code:
taskInfo = new TaskInfo();
taskInfo.getDocInfoList().add(D1);
taskInfo.getDocInfoList().add(D2);
taskInfo.getDocInfoList().add(D3);
taskInfo.setTaskUdId(taskUdId);
//insert into data base
Session session = HibernateUtil.getSession();
Transaction tx = null;
try
{
tx = session.beginTransaction();
session.merge(taskInfo);
tx.commit();
}
catch (Exception e)
{
if(tx != null) tx.rollback();
e.printStackTrace();
}
finally
{
session.close();
}
Please help.
Thanks in advance