-->
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.  [ 6 posts ] 
Author Message
 Post subject: Persist new Suclass Object
PostPosted: Wed Feb 18, 2009 5:18 am 
Newbie

Joined: Wed Feb 18, 2009 4:53 am
Posts: 4
I've got a problem using Spring 2.5.5, Hibernate 3.2.4.sp1 and Postgres.

I'm using a base class (BaseClass) with common data and a set of subclasses with more information, using a table-per-class schema sharing the same id. That works.

There is now the need to specialize one of the subclasses (SubClass). I've created a new subclass of it (SpecializedSubClass).

Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@SequenceGenerator(name = "SEQ_GEN", sequenceName = "baseclass_sequence", allocationSize = 1)
public class BaseClass  {
   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_GEN")
}   protected Long id;

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class
SubClass extends BaseClass {
   @Column
   private String column1;
}

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class
SpecializedSubClass extends SubClass {
   @Column
   private String column2;
}



When I create for the existing objects in the database the new objects that works as expected. But when I try to create a new SpecializedSubClass for an existing SubClass in the runtime I get an error:

Code:
HibernateSystemException: a different object with the same identifier value was already associated with the session


If I evict the SubClass object then I get the following exception:

Code:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.orm.hibernate3.HibernateOptimisticLockingFailureException: Object of class [SpecializedSubClass] with identifier [945]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [SpecializedSubClass#945]


To do it I've created a new SpecializedSubClass object copying all the fields of SubClass and assigning it the same id. Is that a wrong way of doing it? Does anybody know how to do it?

Thanks in advance,
Jordi


Top
 Profile  
 
 Post subject: Ideas?
PostPosted: Wed Feb 18, 2009 9:48 am 
Newbie

Joined: Wed Feb 18, 2009 4:53 am
Posts: 4
Any Ideas or hints are appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 9:56 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Well, your approach is not very "Java-like". You cannot instantiate for example a vehicle-object and later instantiate a subclass of Vehicle saying this subclass-object also is the same object as your vehicle.

There are two alternatives to think about:
1. always create a specialized subclass, as (by polymorphism definition) a "subclass is a class", leaving the extra property null.
2. Use associations instead of inheritance, if objects temporarily have other properties.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:32 am 
Newbie

Joined: Wed Feb 18, 2009 4:53 am
Posts: 4
To be exact that's like "I have a car, and now I have a tunned car". A tunned car has a lot of properties that a normal car doesn't, and I would like to mantain it separated, as any existing car can be transformed every time to a tunned car.
The idea was to load a car, look if it's an instance of tunned car, and change the logic according to that.
I can always create the specialized subclass and have an extra property "isTunned", but I thought it could be better deciding it from the instance type.

Anyway, how would you do it with associations?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 10:42 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
You could create a class TunedProperties (the name could be better ;-)) where you include these special properties. To car you would add an attribute of that class.

So a car is only tuned if this attribute tunedProperties is not null.

An alternative in that case would be to use TunedProperties as @Embeddable, so that these extra properties are stored in the car table. hibernate would set tunedProperties to null then, if any if the properties of that embedded object are null.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2009 11:08 am 
Newbie

Joined: Wed Feb 18, 2009 4:53 am
Posts: 4
That's exactly what I needed, Thanks a lot!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.