-->
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.  [ 9 posts ] 
Author Message
 Post subject: instance not of expected entity type, polymorphism
PostPosted: Sun Mar 30, 2008 11:17 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate</artifactId>
<version>3.2.6.ga</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.3.1.ga</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.3.2.ga</version>
</dependency>

Mapping documents:
JPA
Code between sessionFactory.openSession() and session.close():
Spring
Full stack trace of any exception that occurs:

Name and version of the database you are using:
mySQL 5.05
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

If have the following class hierachy:
trainingscheme has trainingSet
trainingSet has trainingRepetitionBase

trainingRepetitionBase
|
|-TrainingRepetitionDistance
|-TrainingRepetitionRepetitive
|-TrainingRepetitionWeight

So when I change the trainingRepettion from one thing(TrainingRepetitionWeight) to another (TrainingRepetitionDistance
) I get this error:

org.hibernate.HibernateException: instance not of expected entity type: zeuzgroup.core.trainingscheme.TrainingRepetitionDistance is not a: zeuzgroup.core.trainingscheme.TrainingRepetitionBase
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassEntityPersister(AbstractEntityPersister.java:3635)
at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl.java:1347)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:512)
at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:70)
at org.hibernate.impl.SessionImpl.firePersistOnFlush(SessionImpl.java:644)
at org.hibernate.impl.SessionImpl.persistOnFlush(SessionImpl.java:636)
at org.hibernate.engine.CascadingAction$9.cascade(CascadingAction.java:323)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.DefaultPersistEventListener.entityIsPersistent(DefaultPersistEventListener.java:111)

I looked at http://opensource.atlassian.com/project ... se/HHH-667 , which could be related but are already fixed...???


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

what inheritance strategy are you using? How does your db schema look like? And can you post how TrainingRepetitionBase and its subclasses are annotated?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 2:50 pm 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
Im using Joined strategy here they come:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class TrainingRepetitionBase extends BaseEntity {
....}

@Entity
public class TrainingRepetitionWeight extends TrainingRepetitionBase {
...}

@Entity
public class TrainingRepetitionDuration extends TrainingRepetitionBase {
..}

@Entity
public class TrainingRepetitionDistance extends TrainingRepetitionDuration {
..}
@Entity
public class TrainingRepetitionRepetitive extends TrainingRepetitionDuration {
..}


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 3:46 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Looks ok. I assume TrainingRepetitionBase has a @Id annotation as well. What's the deal with BaseEntity though? how is this class mapped? Have you tried a different inheritance strategy?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 30, 2008 4:22 pm 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
hardy.ferentschik wrote:
Looks ok. I assume TrainingRepetitionBase has a @Id annotation as well. What's the deal with BaseEntity though? how is this class mapped? Have you tried a different inheritance strategy?


here comes the content of baseEntity(which contains the id):

@MappedSuperclass
public abstract class BaseEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
/**
* The version number for optimistic locking.
*/
@Version
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer version;
..
}

I have previously tried the different strategies, and found that joined or single were the only ones working, if you refered to to the parent from in a collection(http://opensource.atlassian.com/project ... se/ANN-720

)...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 31, 2008 8:20 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
Could it be something regarding the levels of polymorphism?

parent
|
|-sub1
|
|-sub2
| |-sub2A

Because I only get the error when I use sub2A, not when using sub1 or sub2. Does this shed any lights?


Top
 Profile  
 
 Post subject: Re: instance not of expected entity type, polymorphism
PostPosted: Mon Mar 31, 2008 12:37 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
What exactly do you mean by this:

ninomartinez wrote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

So when I change the trainingRepettion from one thing to another


Can you post some code including the part, that causes the exception?

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject: Re: instance not of expected entity type, polymorphism
PostPosted: Mon Mar 31, 2008 1:11 pm 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
schauder wrote:
What exactly do you mean by this:

ninomartinez wrote:
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

So when I change the trainingRepettion from one thing to another


Can you post some code including the part, that causes the exception?


I'll post the code which causes the exception:

Code:
@Entity
public class TrainingSet extends BaseEntity {

   /**
    * Holds Training information, whether its durative distance or reptition
    * and weight based
    */
   @Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
   @OneToMany(cascade = { CascadeType.ALL }, mappedBy = "trainingSet")
   private List<TrainingRepetitionBase> trainingRepetitions = new ArrayList<TrainingRepetitionBase>();

   @ManyToOne
   private Exercise exercise;

   @ManyToOne
   private TrainingSchemeAbstract trainingScheme;

   public Exercise getExercise() {
      return exercise;
   }

   public void setExercise(Exercise exercise) {
      // manually fix if exercisetype changes
      if (this.exercise==null || this.exercise.getExerciseType().compareTo(
            exercise.getExerciseType()) != 0) {
         TrainingRepetitionBase rep = null;
         // clear always!
         // TODO watchout for orphans!
         trainingRepetitions.clear();
         boolean shouldAdd = false;

         switch (exercise.getExerciseType()) {
         case Distance:
            shouldAdd = true;
[b]            rep = new TrainingRepetitionDistance(new Duration(0, 0, 0), 0,
                  this);[/b]
            break;
         case Repetitive:
            shouldAdd = true;
            [b]rep = new TrainingRepetitionRepetitive(new Duration(0, 0, 0),
                  0, this);[/b]
            break;
         case Weight:
            // shoud do nothing, let the user add reps..!
            break;
         default:
            break;
         }
         if (shouldAdd) {
            [b]trainingRepetitions.add(rep);[/b]
         }

      }
      this.exercise = exercise;
   }

   public List<TrainingRepetitionBase> getTrainingRepetitons() {

      return trainingRepetitions;
   }

   public TrainingSchemeAbstract getTrainingScheme() {
      return trainingScheme;
   }

   public void setTrainingScheme(TrainingScheme trainingScheme) {
      this.trainingScheme = trainingScheme;
   }

   public TrainingSet(Exercise exercise, TrainingSchemeAbstract trainingScheme) {
      super();
      setExercise(exercise);
      this.trainingScheme = trainingScheme;
      trainingScheme.getTrainingSets().add(this);
   }

   public TrainingSet() {
      super();
   }


It's the part in bold that makes the exception..


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 01, 2008 8:36 am 
Beginner
Beginner

Joined: Thu Jan 31, 2008 7:09 am
Posts: 34
At it turns out this error were actually caused by the fact that I did not have and embedded class, and the class declared in persistance xml... So it was actually an class that where unmapped...


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