This week's book giveaways are in the AI and JavaScript forums.
We're giving away four copies each of GANs in Action and WebAssembly in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of GANs in ActionE this week in the AI forum
or WebAssembly in Action in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

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

 
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,
 
What a stench! Central nervous system shutting down. Save yourself tiny ad!
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!