aspose file tools*
The moose likes Object Relational Mapping and the fly likes updating association table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "updating association table" Watch "updating association table" New topic
Author

updating association table

Sneha N Rao
Greenhorn

Joined: Sep 18, 2008
Posts: 4
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

Joined: Sep 18, 2008
Posts: 4
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

Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Well done and thanks for posting your solution for others to share! Oh and welcome to Javaranch


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: updating association table