1 Reply Latest reply on Oct 3, 2006 3:31 AM by Haohua xie

    can a entity be related to a superclass of a joined-inherita

    Haohua xie Newbie

      my enivironment:
      jboss 4.04 GA, jboss-EJB-3.0_RC9-FD
      i use the ejb3 tutorial of joininheritance as the template . i did not chanage any code of Pet.java Cat.java Dog.java.i just add some functions to PetDao and PetDAOBean
      i write a entity, Decorate, referring to Pet. i can insert a entity of Decorate into the database but when i try to get out of the entities of Decorate from the database, some exception happens:


      Decorate.java

      package org.jboss.tutorial.joininheritance.bean;

      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.GenerationType;
      import javax.persistence.Id;
      import javax.persistence.OneToOne;

      @Entity
      public class Decorate implements java.io.Serializable{

      private int id;

      private String name;

      private Pet pet;

      public Decorate() {
      super();

      }

      @Id @GeneratedValue(strategy=GenerationType.AUTO)
      public int getId() {
      return id;
      }

      public String getName() {
      return name;
      }

      @OneToOne(fetch = FetchType.LAZY)
      public Pet getPet() {
      return pet;
      }

      public void setId(int id) {
      this.id = id;
      }

      public void setName(String name) {
      this.name = name;
      }

      public void setPet(Pet pet) {
      this.pet = pet;
      }
      }

      PetDAO.java
      package org.jboss.tutorial.joininheritance.bean;

      import java.util.Collection;
      import java.util.List;

      public interface PetDAO
      {
      Dog createDog(String name, double weight, int bones);

      Cat createCat(String name, double weight, int lives);

      List findByWeight(double weight);

      Decorate createDecorate(String name,Pet pet);

      Collection getDecorate();
      }

      PetDAOBean.java

      package org.jboss.tutorial.joininheritance.bean;

      import java.util.ArrayList;
      import java.util.Collection;
      import java.util.List;

      import javax.ejb.Remote;
      import javax.ejb.Stateless;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      import javax.persistence.Query;

      @Stateless
      @Remote(PetDAO.class)
      public class PetDAOBean implements PetDAO {
      @PersistenceContext
      private EntityManager manager;

      public Dog createDog(String name, double weight, int bones) {
      Dog dog = new Dog();
      dog.setName(name);
      dog.setWeight(weight);
      dog.setNumBones(bones);
      manager.persist(dog);
      return dog;
      }

      public Cat createCat(String name, double weight, int lives) {
      Cat cat = new Cat();
      cat.setName(name);
      cat.setWeight(weight);
      cat.setLives(lives);
      manager.persist(cat);
      return cat;
      }

      public List findByWeight(double weight) {
      return manager.createQuery(
      "select p from Pet p where p.weight < :weight").setParameter(
      "weight", weight).getResultList();
      }

      public Decorate createDecorate(String name, Pet pet) {
      Decorate dec = new Decorate();
      dec.setName(name);
      dec.setPet(pet);
      manager.persist(dec);
      return dec;
      }

      public Collection getDecorate() {
      Collection founds = new ArrayList();
      Query query = manager.createQuery("SELECT o FROM Decorate o");
      List list = query.getResultList();
      for (Object obj : list) {
      if (obj instanceof Decorate) {
      founds.add((Decorate) obj);
      }
      }
      return founds;
      }
      }

      Client.java
      public class Client {
      public static void main(String[] args) throws Exception {
      InitialContext ctx = new InitialContext();
      PetDAO dao = (PetDAO) ctx.lookup("PetDAOBean/remote");

      Pet p1 = dao.createCat("Toonses", 15.0, 9);
      Pet p2 = dao.createCat("Sox", 10.0, 5);
      Pet p3 = dao.createDog("Winnie", 70.0, 5);
      Pet p4 = dao.createDog("Junior", 11.0, 1);
      Decorate d1 = dao.createDecorate("Test", p1);
      Decorate d2 = dao.createDecorate("Test2", p1);

      List l = dao.findByWeight(14.0);
      for (Object o : l) {
      System.out.println(((Pet) o).getName());
      }

      Collection founds = dao.getDecorate(); //exception throws here
      for (Decorate value : founds) {
      System.out.println(value.getName());
      }
      }
      }

      Cat.java

      package org.jboss.tutorial.joininheritance.bean;

      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;

      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public class Cat extends Pet
      {
      int lives;

      public int getLives()
      {
      return lives;
      }

      public void setLives(int lives)
      {
      this.lives = lives;
      }
      }

      Dog.java
      package org.jboss.tutorial.joininheritance.bean;

      import javax.persistence.Entity;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.Inheritance;
      import javax.persistence.InheritanceType;
      import javax.persistence.Entity;

      @Entity
      @Inheritance(strategy = InheritanceType.JOINED)
      public class Dog extends Pet
      {
      private int numBones;

      public int getNumBones()
      {
      return numBones;
      }

      public void setNumBones(int numBones)
      {
      this.numBones = numBones;
      }
      }


      run:
      [java] Sox
      [java] Junior
      [java] Exception in thread "main" java.lang.RuntimeException: org.jboss.tm.
      JBossRollbackException: Unable to commit, tx=TransactionImpl:XidImpl[FormatId=25
      7, GlobalId=xhh/21, BranchQual=, localId=21] status=STATUS_NO_TRANSACTION; - nes
      ted throwable: (javax.persistence.PersistenceException: org.hibernate.HibernateE
      xception: instance not of expected entity type: org.jboss.tutorial.joininheritan
      ce.bean.Pet_$$_javassist_10 is not a: org.jboss.tutorial.joininheritance.bean.Pe
      t)
      [java] at org.jboss.aspects.tx.TxPolicy.handleEndTransactionException(T
      xPolicy.java:198)
      [java] at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:18
      0)
      [java] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)

      [java] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxIntercep
      tor.java:197)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropag
      ationInterceptor.java:76)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(
      StatelessInstanceInterceptor.java:62)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(A
      uthenticationInterceptor.java:78)
      [java] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(
      Ejb3AuthenticationInterceptor.java:131)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagatio
      nInterceptor.java:47)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(As
      ynchronousInterceptor.java:106)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(Sta
      telessContainer.java:263)
      [java] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
      [java] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invok
      e(AOPRemotingInvocationHandler.java:82)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:82
      8)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:68
      1)
      [java] at org.jboss.remoting.transport.socket.ServerThread.processInvoc
      ation(ServerThread.java:358)
      [java] at org.jboss.remoting.transport.socket.ServerThread.dorun(Server
      Thread.java:412)
      [java] at org.jboss.remoting.transport.socket.ServerThread.run(ServerTh
      read.java:239)
      [java] Caused by: org.jboss.tm.JBossRollbackException: Unable to commit, tx
      =TransactionImpl:XidImpl[FormatId=257, GlobalId=xhh/21, BranchQual=, localId=21]
      status=STATUS_NO_TRANSACTION; - nested throwable: (javax.persistence.Persistenc
      eException: org.hibernate.HibernateException: instance not of expected entity ty
      pe: org.jboss.tutorial.joininheritance.bean.Pet_$$_javassist_10 is not a: org.jb
      oss.tutorial.joininheritance.bean.Pet)
      [java] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:372)

      [java] at org.jboss.tm.TxManager.commit(TxManager.java:240)
      [java] at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:17
      5)
      [java] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)

      [java] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxIntercep
      tor.java:197)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropag
      ationInterceptor.java:76)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(
      StatelessInstanceInterceptor.java:62)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(A
      uthenticationInterceptor.java:78)
      [java] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(
      Ejb3AuthenticationInterceptor.java:131)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagatio
      nInterceptor.java:47)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(As
      ynchronousInterceptor.java:106)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(Sta
      telessContainer.java:263)
      [java] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
      [java] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invok
      e(AOPRemotingInvocationHandler.java:82)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:82
      8)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:68
      1)
      [java] at org.jboss.remoting.transport.socket.ServerThread.processInvoc
      ation(ServerThread.java:358)
      [java] at org.jboss.remoting.transport.socket.ServerThread.dorun(Server
      Thread.java:412)
      [java] at org.jboss.remoting.transport.socket.ServerThread.run(ServerTh
      read.java:239)
      [java] at org.jboss.remoting.RemoteClientInvoker.invoke(RemoteClientInv
      oker.java:190)
      [java] at org.jboss.remoting.Client.invoke(Client.java:525)
      [java] at org.jboss.remoting.Client.invoke(Client.java:488)
      [java] at org.jboss.aspects.remoting.InvokeRemoteInterceptor.invoke(Inv
      okeRemoteInterceptor.java:55)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.tx.ClientTxPropagationInterceptor.invoke(Cl
      ientTxPropagationInterceptor.java:61)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.security.SecurityClientInterceptor.invoke(S
      ecurityClientInterceptor.java:55)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInte
      rceptor.java:77)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessRemoteProxy.invoke(Stateles
      sRemoteProxy.java:102)
      [java] at $Proxy0.getDecorate(Unknown Source)
      [java] at org.jboss.tutorial.joininheritance.Client.Client.main(Client.
      java:56)
      [java] Caused by: javax.persistence.PersistenceException: org.hibernate.Hib
      ernateException: instance not of expected entity type: org.jboss.tutorial.joinin
      heritance.bean.Pet_$$_javassist_10 is not a: org.jboss.tutorial.joininheritance.
      bean.Pet
      [java] at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceE
      xception(AbstractEntityManagerImpl.java:641)
      [java] at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletio
      n(AbstractEntityManagerImpl.java:524)
      [java] at org.jboss.tm.TransactionImpl.doBeforeCompletion(TransactionIm
      pl.java:1491)
      [java] at org.jboss.tm.TransactionImpl.beforePrepare(TransactionImpl.ja
      va:1110)
      [java] at org.jboss.tm.TransactionImpl.commit(TransactionImpl.java:324)

      [java] at org.jboss.tm.TxManager.commit(TxManager.java:240)
      [java] at org.jboss.aspects.tx.TxPolicy.endTransaction(TxPolicy.java:17
      5)
      [java] at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:87)

      [java] at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxIntercep
      tor.java:197)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropag
      ationInterceptor.java:76)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(
      StatelessInstanceInterceptor.java:62)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.aspects.security.AuthenticationInterceptor.invoke(A
      uthenticationInterceptor.java:78)
      [java] at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(
      Ejb3AuthenticationInterceptor.java:131)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagatio
      nInterceptor.java:47)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(As
      ynchronousInterceptor.java:106)
      [java] at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInv
      ocation.java:101)
      [java] at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(Sta
      telessContainer.java:263)
      [java] at org.jboss.aop.Dispatcher.invoke(Dispatcher.java:106)
      [java] at org.jboss.aspects.remoting.AOPRemotingInvocationHandler.invok
      e(AOPRemotingInvocationHandler.java:82)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:82
      8)
      [java] at org.jboss.remoting.ServerInvoker.invoke(ServerInvoker.java:68
      1)
      [java] at org.jboss.remoting.transport.socket.ServerThread.processInvoc
      ation(ServerThread.java:358)
      [java] at org.jboss.remoting.transport.socket.ServerThread.dorun(Server
      Thread.java:412)
      [java] at org.jboss.remoting.transport.socket.ServerThread.run(ServerTh
      read.java:239)
      [java] Caused by: org.hibernate.HibernateException: instance not of expecte
      d entity type: org.jboss.tutorial.joininheritance.bean.Pet_$$_javassist_10 is no
      t a: org.jboss.tutorial.joininheritance.bean.Pet
      [java] at org.hibernate.persister.entity.AbstractEntityPersister.getSub
      classEntityPersister(AbstractEntityPersister.java:3568)
      [java] at org.hibernate.impl.SessionImpl.getEntityPersister(SessionImpl
      .java:1347)
      [java] at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java
      :180)
      [java] at org.hibernate.engine.CascadingAction$9.noCascade(CascadingAct
      ion.java:347)
      [java] at org.hibernate.engine.Cascade.cascade(Cascade.java:139)
      [java] at org.hibernate.event.def.AbstractFlushingEventListener.cascade
      OnFlush(AbstractFlushingEventListener.java:130)
      [java] at org.hibernate.event.def.AbstractFlushingEventListener.prepare
      EntityFlushes(AbstractFlushingEventListener.java:121)
      [java] at org.hibernate.event.def.AbstractFlushingEventListener.flushEv
      erythingToExecutions(AbstractFlushingEventListener.java:65)
      [java] at org.hibernate.event.def.DefaultFlushEventListener.onFlush(Def
      aultFlushEventListener.java:26)
      [java] at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
      [java] at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:
      338)
      [java] at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletio
      n(AbstractEntityManagerImpl.java:515)
      [java] ... 27 more
      [java] Java Result: 1