• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

updating association table

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I ve recently started working on Hibernate. I have queries related to persisting collections and their associations.

My application has two POJOs FacilityBean and FloorBean mapping to entities Facility and Floor. FacilityBean has a set of floors as I want the set of floors associated with the facility to be retrieved along with it. And Floor has a Facility object as every floor belongs to a facility (there are no foreign keys involved).

Mapping Xml is like this:

Facility :
<hibernate-mapping default-access="field">
<class name="com.spring.orm.FacilityBean" table="fm_facility_master">
<id name="Id" column="FM_FACILITY_ID" >
<generator class="native" />
</id>
<property name="parent" column="FM_FACILITY_PARENT_ID" />
......
<property name="default_loc" column="FM_DEFAULT_LOC" />

<set name="floors" lazy="false" table="facility_floor" cascade="none">
<key column="FM_FACILITY_ID"/>
<many-to-many column="FL_FLOOR_ID" unique="true"
class="com.spring.orm.FloorBean"/>
</set>
</class>
</hibernate-mapping>

Floor:

<hibernate-mapping default-access="field">
<class name="com.spring.orm.FloorBean" table="fl_floor">
<id name="Id" column="FL_FLOOR_ID" >
<generator class="native" />
</id>
<property name="floorName" type="java.lang.String" column="FL_FLOOR_NAME"/>
<property name="facilityId" column="FL_FACILITY_ID"/>

<join table="facility_floor" inverse="true" optional="true">
<key column="FL_FLOOR_ID"/>
<many-to-one name="facility" column="FM_FACILITY_ID" cascade="all" not-null="true"/>
</join>
</class>
</hibernate-mapping>

I can persist the collection only when i am inserting both the entities together after setting the set of floors.
I want to insert a new floor alone and also update the association table facility_floor like insert new floor object and the FM_FACILITY_ID and the FL_FLOOR_ID fields gets updated in the association table facility_floor.


Can anyone please help me to know how I can do this.
I believe that hibernate will insert data into the association join table only when new entries of the entities like facility and floor are inserted together. Is there a way where I can insert floor object with existing facility and persist this as floors are added later to a facility.
Please help.
Thanks in advance,
Viola

[ September 18, 2008: Message edited by: viola rotrigues ]
[ September 18, 2008: Message edited by: viola rotrigues ]
 
Sneha N Rao
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi friends,
I found out how to do it now.
This association is nothing but a parent and child association. And child and parent are both associated hence there is no change in the POJOs. Slight changes in the mapping xml.

Mapping xml:
Floor-
<hibernate-mapping default-access="field">
<class name="com.spring.orm.FloorBean" table="fl_floor">
<id name="Id" column="FL_FLOOR_ID" >
<generator class="native" />
</id>
<many-to-one name="facility" class="com.spring.orm.FacilityBean" column="FL_FACILITY_ID"/>
</class>
</hibernate-mapping>

The column that stores the facility id is a association with Facility object in the FloorBean class.

Facility-
<hibernate-mapping default-access="field">
<class name="com.spring.orm.FacilityBean" table="fm_facility_master">
<id name="Id" column="FM_FACILITY_ID" >
<generator class="native" />
</id>
<set name="floors" inverse="true" lazy="false" cascade="none">
<key column="FL_FACILITY_ID"/>
<one-to-many class="com.spring.orm.FloorBean"/>
</set>
</class>
</hibernate-mapping>

By setting inverse="true" and cascade="none" both parent and child can be inserted independently. And association still persists.
This is the link where associations are explained very well.
http://www.neeraj.name/43/hibernate-association-behind-the-scene-action

 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well done and thanks for posting your solution for others to share! Oh and welcome to Javaranch
 
crispy bacon. crispy tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic