-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 
Author Message
 Post subject: Updating from one subclass to another subclass
PostPosted: Fri Oct 27, 2006 3:20 am 
Newbie

Joined: Fri Oct 27, 2006 2:45 am
Posts: 5
Location: Bangalore
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("sonu@yahoo.com");
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 8:15 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
Code:
session.update(book);

Create a new book?

If you want change Insurence, dont create new Insurence. Use:
Code:
Insurence insurence = (Insurence) session.get(Book.class, 34);

_________________
Please don't forget to rateME!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 3:20 am 
Newbie

Joined: Fri Oct 27, 2006 2:45 am
Posts: 5
Location: Bangalore
It gives class cast exception from changing from Book to Insurance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 3:24 am 
Newbie

Joined: Fri Oct 27, 2006 2:45 am
Posts: 5
Location: Bangalore
If you want change Insurence, dont create new Insurence. Use:
Code:
Insurence insurence = (Insurence) session.get(Book.class, 34);
[/quote]

It gives Class Cast Exception when Changing from Book to Insurance !!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 7:49 am 
Newbie

Joined: Fri Oct 27, 2006 2:45 am
Posts: 5
Location: Bangalore
actually my problem ia same as this topic problem

http://forum.hibernate.org/viewtopic.ph ... highlight=


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 7:41 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
id 34 in your database is a Book our a Insurence ?

_________________
Please don't forget to rateME!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 7:46 am 
Newbie

Joined: Fri Oct 27, 2006 2:45 am
Posts: 5
Location: Bangalore
its Book .


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 03, 2006 8:01 am 
Beginner
Beginner

Joined: Thu Sep 22, 2005 10:48 am
Posts: 30
Location: Rio de Janeiro, Brazil
I´m sorry, I don´t ready well. I think change Book to Insurence is not possibel because this classes are brothers.

What problem you delete book and insert new Insurence?

PS: I study this case and post new mesage

_________________
Please don't forget to rateME!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.