-->
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.  [ 2 posts ] 
Author Message
 Post subject: Polymorphism,session.get and "not of the specified subc
PostPosted: Fri Jun 13, 2008 2:08 am 
Newbie

Joined: Tue May 27, 2008 12:35 pm
Posts: 8
Hibernate version: 3.2.6 GA

Name and version of the database you are using: SQL Server 2005


Imagine these two hypothetical class hierarchies...

Code:
    Automobile
            |
    |-----------|
Car          Truck



       TireType
            |
    |-----------|
Offroad     Onroad




Code:
@Entity
@Table(name = "automobile")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class Automobile
{
   @Transient
    public abstract TireType getTireType();
}

@Entity
@DiscriminatorValue("CAR")
public class Car extends Automobile
{
        private Onroad tireType;

   @ManyToOne(cascade = {})
   @NotFound(action = NotFoundAction.IGNORE)
   @JoinColumn(name = "tire_type_id")
   @NotNull
   public Onroad getTireType()
   {
      return tireType;
   }
}



@Entity
@DiscriminatorValue("TRUCK")
public class Truck extends Automobile
{
        private Offroad tireType;

   @ManyToOne(cascade = {})
   @NotFound(action = NotFoundAction.IGNORE)
   @JoinColumn(name = "tire_type_id")
   @NotNull
   public Offroad getTireType()
   {
      return tireType;
   }
}





Code:


@Entity
@Table(name = "tire_type")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "discriminator", discriminatorType = DiscriminatorType.STRING)
public abstract class TireType
{
....
}


@Entity
@DiscriminatorValue("ON")
public class Onroad extends TireType
{
  ....
}


@Entity
@DiscriminatorValue("OFF")
public class Offroad extends TireType
{
  ....
}





Now let's say Automobile #1 is a [Car] with an [Onroad] tire type. If I call the following:

session.get(Automobile.class, 1);

I get an Exception like:
Code:
org.hibernate.WrongClassException: Object with id: 1 was not of the specified subclass: Offroad (Discriminator: ON)


It's basically saying that my Car's tiretype property is mapped as an Offroad tire type but pointing to an Onroad one. However, is NOT. The message shows the wrong class type, but the correct discriminator.

This DOES work.
session.get(Car.class, 1);

I've quadruple checked the database, and the discriminators are all correct. Did I hit a limitation in the polymorphic capabilities or should I be able to do things like this? The reality of my structure is a bit more complicated (more subclass layers, etc.). I've gone through it all extensively, but I'm out of ideas.

Any suggestions? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 16, 2008 12:06 am 
Newbie

Joined: Tue May 27, 2008 12:35 pm
Posts: 8
Ok, let me try this another way. If I have two subclasses of a common superclass, and each of these subclasses have an association property of the same name but of a different type, Hibernate seems to get confused. It'll often seem to get them mixed up, trying to instantiate the wrong type on the association and throw an exception when I ask the session for an object, but specify the superclass instead of the subclass..

Should it be possible to do this? Anything special that needs to be done for this situation? Please reference my first post in the thread for further detaisl if necessary.

Thanks


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