-->
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: entity inheritance and ClassCastException
PostPosted: Mon Apr 18, 2011 8:06 am 
Newbie

Joined: Mon Apr 18, 2011 7:42 am
Posts: 2
Hi. In my code I have inheritance structure as follows:

Code:

@Table(name = "TD_TASKS")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("")
@ForceDiscriminator
public abstract class TaskVO implements Serializable, Cloneable, EntityVO, TaskVOInterface {

   @Id
   @SequenceGenerator(name = "TD_TASKS_SEQ", sequenceName = "TD_TASKS_SEQ", allocationSize = 1)
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "TD_TASKS_SEQ")
   protected Long id;

...
}

@Entity
@DiscriminatorValue("CyclicTaskVO")
@ForceDiscriminator
public class CyclicTaskVO extends TaskVO implements CyclicTaskVOInterface {
...
}

@Entity
@DiscriminatorValue("OrdinaryTaskVO")
@ForceDiscriminator
public class OrdinaryTaskVO extends TaskVO implements OrdinaryTaskVOInterface {
...
}

public interface EntityVO {
   Long getId();
}

public interface TaskVOInterface extends EntityVO {
...
}

public interface CyclicTaskVOInterface extends TaskVOInterface {
...
}

public interface OrdinaryTaskVOInterface extends TaskVOInterface {
...
}




TaskDao uses generic Dao method :

Code:
   public T get(long id) {
      Class<T> modelDataClass = getModelDataClass();
      Object model = getCurrentSession().get(modelDataClass, id);
      return modelDataClass.cast(model);
   }


in my case modelDataClass is set to taskVO, so the method works like :
return (TaskVO) getCurrentSession().get(TaskVO.class, id);

In some cases get method returns instances of CyclicTaskVO and OrdinaryTaskVO, but sometimes I get class that is
instance of proxy: TaskVO_$$_javassist_ 21. This instance cannot be cast to any of classes CyclicTaskVO or OrdinaryTaskVO.
I also cant cast it to OrdinaryTaskVOInterface or CyclicTaskVOInterface.
Why in some cases hibernate does not use DiscriminatorColumn value (which is set properly)? I get proxy to abstract class
which is useless for me.

Thanks for your help.


Top
 Profile  
 
 Post subject: Re: entity inheritance and ClassCastException
PostPosted: Thu Apr 21, 2011 3:14 am 
Newbie

Joined: Mon Apr 18, 2011 7:42 am
Posts: 2
anyone ? ..


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.