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.
I want to insert a new floor seperately and also update the association table facility_floor like insert new floor object and the FacilityId and the new FloorID gets written into to the association table.
Can anyone please help me to know how can i 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.
Please help.
|