After a day of googling, reading the docs multiple times and checking the forms we can't seem to find the correct mapping for the following scenario:
Before presenting the issue I'd like to note that the database schema is fixed and cannot be changed.
Tables:
Code:
CREATE TABLE CYCLE (
CYCLE_ID BIGINT CONSTRAINT CYCLE_PK PRIMARY KEY,
TITLE NVARCHAR(256) NOT NULL,
DESCRIPTION NVARCHAR(256) NULL,
)
CREATE TABLE CYCLE_COMP_TYPE (
CYCLE_ID BIGINT NOT NULL,
COMP_TYPE_ID BIGINT NOT NULL,
CYCLE_SETTING NVARCHAR(MAX),
PRIMARY KEY (CYCLE_ID, COMP_TYPE_ID)
)
Java:
Code:
public class Cycle {
private Set<CycleCompType> cycleCompTypes = new HashSet<CycleCompType>();
private String title;
private String description;
public void addCycleCompType(CycleCompType cycleCompType) {
cycleCompType.setCycle(this);
cycleCompTypes.add(cycleCompType);
}
... the rest is omitted for brevity sake
}
public class CycleCompType {
private String compCycleSetting;
private Cycle cycle;
private Long cycleId;
... the rest is omitted for brevity sake
}
Mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.rf.cmp.data.cycle">
<class name="Cycle" table="WE_COMP_CYCLE">
<id name="id" type="long" column="CYCLE_ID" unsaved-value="0">
<generator class="com.XXXX.orm.HibernateSequenceIdGenerator">
<param name="SEQUENCE_NAME">COMP_CYCLE_SEQ</param>
</generator>
</id>
<set name="cycleCompTypes" lazy="false" inverse="true" cascade="all-delete-orphan">
<key column="CYCLE_ID" not-null="true"/>
<one-to-many class="CycleCompType"/>
</set>
<property name="title" type="string" column="TITLE"/>
<property name="description" type="string" column="DESCRIPTION"/>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.rf.cmp.data.cycle">
<class name="CycleCompType" table="CYCLE_COMP_TYPE">
<composite-id>
<key-property name="cycleId" column="CYCLE_ID" type="long"/> - needed here to execute updates ?
<key-property name="compTypeId" column="COMP_TYPE_ID" type="long"/>
</composite-id>
<many-to-one name="cycle" column="CYCLE_ID" not-null="true" class="Cycle" update="true"
insert="true" /> - needed here for cascade insert ?
<property name="compCycleSetting" type="string" column="COMP_CYCLE_SETTING"/>
</class>
</hibernate-mapping>
Our desired use cases are:
Code:
1) insert Cycle, cascade cycleId to CycleCompType and insert CycleCompType - currently fails since we have cycleId double mapped
CycleCompType cct = new CycleCompType();
Cycle cycle = new Cycle();
cycle.addCycleCompType(cct);
session.save(cycle);
2) delete cycle - deletes cycle and child entries - seems to work
3) update cycle - updates' cycle and child entries including: collection add's, collection removes
Any help with setting up the correct mapping is greatly appreciated