-->
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.  [ 5 posts ] 
Author Message
 Post subject: Discriminator value doesn't change
PostPosted: Tue Aug 09, 2005 4:44 am 
Newbie

Joined: Tue Aug 09, 2005 4:01 am
Posts: 7
Hibernate version:

3

Mapping documents:

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 default-lazy="false" default-cascade="all-delete-orphan">
    <class name="Interpret" table="interprets">
        <id name="persistenceId" column="id" unsaved-value="0" type="java.lang.Long">
      <generator class="increment"/>
   </id>
      
   <discriminator column="type" type="java.lang.String"/>
      
   <map name="levelsAsMap" table="interpret_levels">
            <key column="interpret_id" not-null="true"/>
            <map-key column="level" type="java.lang.String"/>
            <element column="value" type="java.lang.Double" not-null="true"/>
        </map>      
      
   <subclass name="AscendingInterpret" discriminator-value="ascending">
   </subclass>
   <subclass name="DescendingInterpret" discriminator-value="descending">
   </subclass>
   <subclass name="BandInterpret" discriminator-value="band">      
      </subclass>
    </class>   
</hibernate-mapping>


Name and version of the database you are using:

MySQL

Description of the problem:

I have an abstract Interpret class. When I create a concrete interpret (Ascending/Descending/...) and I save it to the database the object is saved correctly. Loading works fine too. But in the client application I also need to change interpret's type when needed. Here comes the problem. Hibernate doesn't save the changes to the discriminator value. It updates correctly the level map but the type column contains always the type of the first object saved.

What is specific is that I preserve the persistence ID and try to save the new object (which is of different concrete class). If I create a new object each time there is a problem with another mapping in the hierarchy. I have a Personal Interpret which uses the persistance ID of the interpret like this:

Code:
        <id name="persistenceId" column="id" unsaved-value="0" type="java.lang.Long">
                <generator class="foreign">
                        <param name="property">interpret</param>
                </generator>
        </id>


When I save the interpret as a new object the PersonalInterpret's ID doesn't follow the change and is left linked with the first interpret. Anyway I would like to work with the old persistance ID and change only the type class. This way I have less things to do.

I thought about using property instead of discriminator but I could not really change the class type in the load.

So, finally, anyone has an idea why Hibernate doesn't change the discriminator-value when the subclass changes it's type?

_________________
Regards,
Alexander Kamburov

wise_guybg at yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 09, 2005 1:31 pm 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
I think you can´t. As you can´t create a Number object and them turn it into a Integer or Double. An object is bounded to the type. Even if you can update the discriminator column in the database, it will lead to an error soon or later.
If your object can change the class, it´s not a class at all. Try to make is as an attribute with a enumerated type. You can them implement specific logic in the enumerated class.

Good Luck.
If you don´t hate my answer, rate it :-)

_________________
Alexandre Torres
--------------------


Top
 Profile  
 
 Post subject: Sorry for the rate :/
PostPosted: Wed Aug 10, 2005 8:42 am 
Newbie

Joined: Tue Aug 09, 2005 4:01 am
Posts: 7
Actually in the Java code I am not trying to change the class of the object, I create a new object. But in Hibernate it really looks like trying to change the class. Yes, my logic isn't really clear in the persistance part and that's why Hibernate doesn't obey my orders.

10x Atorres for the comment. If you post a reply I'll try to put a positive this time.

_________________
Regards,
Alexander Kamburov

wise_guybg at yahoo.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 9:42 am 
Regular
Regular

Joined: Mon Jun 14, 2004 1:42 pm
Posts: 80
Location: Brazil
You may try a hack: If you change the discriminator externaly, and them renew the session (by flushing), and reload the object from database, hibernate should not complain about the class change.
But changing the class in the same session will cause an error if some object references it. You will need a new java object to change the class and any reference to your object will need to be updated. This will lead to an error.
I think that if you can model it without class hierarchy it will be more realiable.

_________________
Alexandre Torres
--------------------


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 10, 2005 11:06 am 
Newbie

Joined: Tue Aug 09, 2005 4:01 am
Posts: 7
In fact I was thinking of adding an update statement to change the type column according to the class of the interpret that is saved.

Anyway finally I decided to use another approach. I have the type as a property in the PersonalInterpret so this class in fact acts as a facade to use in Hibernate. After persistance is done the application uses the Interpret object (each concrete class with its logic) which is contained in the Personal interpret.

Thank you for the reflexions.

_________________
Regards,
Alexander Kamburov

wise_guybg at yahoo.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.