Hibernate does not work correctly in case a set references to a
class with composite id. It issues not an insert, but update statement for the referenced class.
Note: in case of simple PKs hibernate works fine (see alternative sections below).
Hibernate version:
2.1.8 / 3.0 rc1
Mapping documents:
<class name="Server" table="SERVER">
<id name="serverId" type="java.lang.Integer" column="SERVER_ID">
<generator class="sequence">
<param name="sequence">fmsmgr.SERVER_ID_SEQ</param>
</generator>
</id>
<property name="url" column="URL" type="java.lang.String" not-null="true" unique="true" length="256" />
<property name="createDt" column="CREATE_DT"
type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT"
type="java.util.Date" insert="false" update="false" />
<set name="attributes" inverse="false" cascade="all-delete-orphan">
<key>
<column name="SERVER_ID" />
</key>
<one-to-many class="ServerAttribute" />
</set>
</class>
<class name="ServerAttribute" table="SERVER_ATTRIBUTE">
<composite-id name="PK" class="ServerAttributePK">
<key-property name="serverId" column="SERVER_ID" type="java.lang.Integer" length="6" />
<key-property name="name" column="NAME" type="java.lang.String" length="100"/>
</composite-id>
<property name="value" column="VALUE" type="java.lang.String" length="256" />
<property name="createDt" column="CREATE_DT" type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT" type="java.util.Date" insert="false" update="false" />
<many-to-one name="server" column="SERVER_ID" class="Server" insert="false" update="false" not-null="true" />
</class>
Alternative mapping
<class name="ServerAttribute" table="SERVER_ATTRIBUTE">
<id name="id" type="java.lang.Integer" column="SERVER_ATTRIBUTE_ID">
<generator class="sequence">
<param name="sequence">fmsmgr.SERVER_ID_SEQ</param>
</generator>
</id-->
<property name="value" column="VALUE" type="java.lang.String" length="256" />
<property name="createDt" column="CREATE_DT" type="java.util.Date" insert="false" update="false" />
<property name="modDt" column="MOD_DT" type="java.util.Date" insert="false" update="false" />
<many-to-one name="server" column="SERVER_ID" class="Server" insert="false" update="false" not-null="true" />
</class>
Code between sessionFactory.openSession() and session.close():
Server server = new Server();
server.setUrl("sadasdsada");
ServerAttribute attribute = new ServerAttribute(new ServerAttributePK(null, "name"));
attribute.setValue("bsdsadsa");
attribute.setServer(server);
server.getAttributes().add(attribute);
session.save(server);
session.flush();
Alternative code
Server server = new Server();
server.setUrl("sadasdsada");
ServerAttribute attribute = new ServerAttribute();
attribute.setValue("bsdsadsa");
attribute.setServer(server);
server.getAttributes().add(attribute);
session.save(server);
session.flush();
The generated SQL (show_sql=true):
insert into SERVER (URL, SERVER_ID) values (?, ?)
update SERVER_ATTRIBUTE set VALUE=? where SERVER_ID=? and NAME=?
update SERVER_ATTRIBUTE set SERVER_ID=? where SERVER_ID=? and NAME=?
Alternative sql
insert into SERVER (URL, SERVER_ID) values (?, ?)
insert into SERVER_ATTRIBUTE (VALUE, SERVER_ATTRIBUTE_ID) values (?, ?)
|