• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inserting into One-to-Many & Many-to-One related tables

 
Mohan M
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Currently we are working on a project with Hibernate + Webworks. Need your suggestion in this scenario as we are unable to find a solution while trying the values entered by users in a JSP to tables with one-to-many & many-to-one relationship.

Here are details for your reference..

We would be really greatful if you can suggest how to rectify or implement this.

A. Database Schema
----------------------------
User_Table SavedSearch_Master
------------- -------------------------------
User_Id Integer (PK) SS_Id Integer (PK)
Username varchar one--->many User_Id (PFK)
Password varchar Search_Name varchar

1
|
|
^
many

ActTypeComp_Map
---------------------------
AC_Id Integer (PK)
Act_Count Integer
SS_Id Integer(FK)
Type_Id Integer(FK)
Comp_Id Integer(FK)

many many
one one
Act_Type Act_Comp
------------------ ----------------
Type_Id Integer (PK) Comp_Id Integer (PK)
TypeDesc varchar CompDesc varchar

B. Relationship/Associations in hbm.xml files
--------------------------------------------------------

1. UserTable.hbml.xml
<set name="savedSearchMaster" table="SAVEDSEARCH_MASTER" inverse="true" lazy="true">
<key column="user_id"/>
<one-to-many class="com....model.SavedSearchMaster"/>
</set>

2. SavedSearchMaster.hbm.xml
<many-to-one name="userTable" column="user_id" class="com.....model.UserTable"/>
<set name="ActTypComp_Map" table="ACTTYPCOMP_MAP" inverse="true" lazy="true">
<key column="ss_id"/>
<one-to-many class="com....model.ActTypCompMap/>
</set>

3. ActType.hbm.xml
<many-to-one name="actType" column="type_id" class="com.....model.ActType"/>

4. ActComp.hbm.xml
<many-to-one name="actComp" column="comp_id" class="com.....model.ActComp"/>


C. Hibernate Implementation in Action Class
------------------------------------------------------
// This piece of code WORKS FINE for inserting the values into SavedSearch_Master table
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

try {
SavedSearchMaster ssm = new SavedSearchMaster();
// getSession() returns the Hibernate session from AbstractAction
userTable = (UserTable) getSession().get(UserTable.class, userId);
ssm.set(searchName);
ssm.setUserTable(userTable);

getSession().save(ssm);
getSession().flush();
}catch(Exception ex){
}

// When I modified the above code so as to insert values into SavedSearch_Master
// as well as ActTypComp_Map, I am not able to insert values into ActTypComp_Map
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
try {
SavedSearchMaster ssm = new SavedSearchMaster();
// getSession() returns the Hibernate session from AbstractAction
userTable = (UserTable) getSession().get(UserTable.class, userId);
ssm.set(searchName);

/// New Code ///////////////////////////////
Set actSet = new HashSet();

for(int i =0; i < actTypes; i++){

actTypeCompMap = new ActTypeCompMap();
actType = new ActType();
actComp = new ActComp();

actType = getSession().get(ActType.class, actTypeId);
actComp = getSession().get(ActComp.class, actCompId);

act_count = ...
// set the value of count in Map table
actTypeCompMap.setActCount(act_count);
// set the FK values by setting the corresponding objects
actTypeCompMap.setActType(actType);
actTypeCompMap.setActComp(actComp);
// add actTypeCompMap object to a Set
actSet.add(actTypeCompMap);
}
////////////////////////////////////////////
ssm.setUserTable(userTable);

getSession().save(ssm);
getSession().flush();
}catch(Exception ex){
}

Thanks,
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic