I have a "Table per class hierarchy" with mapping as follows ....
<?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>
<class name="tutorial.hibernate.Contact" table="CONTACT"
lazy="false" discriminator-value="CONT" >
<id name="id" column="ID" type="long">
<generator class="sequence">
<param name="sequence">seq_contact</param>
</generator>
</id>
<discriminator column="DIS" type="string" />
<property name="firstName">
<column name="FIRSTNAME" />
</property>
<property name="lastName">
<column name="LASTNAME" />
</property>
<property name="email">
<column name="EMAIL" />
</property>
<subclass name="tutorial.hibernate.Book" discriminator-value="BOOK">
<property name="strBookName" type="string">
<column name="BOOKNAME" />
</property>
</subclass>
<subclass name="tutorial.hibernate.Insurance" discriminator-value="INSU">
<property name="insuranceName">
<column name="INSURANCE_NAME" />
</property>
<property name="investementAmount">
<column name="INVESTED_AMOUNT" />
</property>
<property name="investementDate">
<column name="INVESTEMENT_DATE" />
</property>
</subclass>
</class>
</hibernate-mapping>
i have a record entry in database for "BOOK" . Now I want to change from
"Book" entity to "Insurance" entity from the GUI .
My sample code for updating record is as follows .....
public class Example {
public static void main(String[] args) {
Session session = null;
try {
SessionFactory sessionFactory = new configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
//assume id is 34
// fetch the Book entity for id "34"
Book book = (Book) session.get(Book.class, 34);
//Changing from Book to Insurence
Insurence insurence = new Insurence();
insurence .setId(book.getId);
insurence .setFirstName("Sonu");
insurence .setLastName("Kurien");
insurence .setEmail("
[email protected]");
insurence .setInsuranceName("SONU");
session.update(book);
tx.commit();
}
} catch (Exception e) {
System.out.println(e);
} finally {
session.flush();
session.close();
}
}
}
After executing this example new record is added instead of updating the old record .
Both Book and Insurance are subclass of Contact.
My objective is to update From Book entity to Insurance Entity . I need the "discriminator-value" also to be changed accordingly