How do I override a sequence generated PK identifier in the example below? The two subclasses below are mutual exclusive. AFAIK I have to delete the old "Checkdomain" object to turn it to a "Checkmaxlength" object. I would like to keep the old PK value for the new object.
Please help, I need to clarify this urgently.
Hibernate version:
2.1.8
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping package="com.abc">
<class name="Checkformat" table="CHECKFORMAT">
<id
column="ID"
name="Id"
type="integer"
>
<generator class="sequence">
<param name="sequence">GEN_CHECKFORMAT_ID</param>
</generator>
</id>
<property
column="COMMENT"
length="1000"
name="Comment"
not-null="false"
type="string"
/>
<joined-subclass name="Checkdomain" table="CHECKDOMAIN">
<key column="ASSIGNMENTID" />
<property
column="MAXVALUE"
length="10"
name="Maxvalue"
not-null="true"
type="integer"
/>
<property
column="MINVALUE"
length="10"
name="Minvalue"
not-null="true"
type="integer"
/>
</joined-subclass>
<joined-subclass name="Checkmaxlength" table="CHECKMAXLENGTH">
<key column="ASSIGNMENTID" />
<property
column="MAXLENGTH"
length="5"
name="Maxlength"
not-null="true"
type="java.lang.Short"
/>
</joined-subclass>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Code:
Transaction trans = session.beginTransaction();
Checkdomain cfd = new Checkdomain();
cfd.setComment("My new Checkformat");
cfd.setMaxvalue(10);
cfd.setMinvalue(2);
Integer id = (Integer) session.save(cfd);
// Now the user decides to change the data to Checkmaxlength.
// Maybe even later in a different transaction.
CheckmaxLength cml = new Checkmaxlength();
cml.setId(id);
cml.setComment(cfd.getComment());
cml.setMaxlength(100);
// Since both objects are subclass of Checkformat, we have to delete
// the old object in order to add the new one
// (these role tables are mutual exclusive)
session.delete(cfd);
session.save(cml);
trans.commit();
Full stack trace of any exception that occurs:Name and version of the database you are using:The generated SQL (show_sql=true):Debug level Hibernate log excerpt:[/code]