Spring 3.1 Hibernate 4 exception for Inheritance [cannot be cast to org.hibernate.mapping.RootClass]

前端 未结 4 767
一生所求
一生所求 2020-11-29 09:42

Hi I have just started using Spring , with Hibernate4 and maven. Basically my class hierarchy is HUmanMicroTask extends from MicroTask . In future there may be several other

相关标签:
4条回答
  • 2020-11-29 10:23

    For TABLE_PER_CLASS hierarchy, the child class inherit the unique key from the parent and so we should not mention unique key constraint in child entity.

    Remove the primary key annotations and this worked well for me.

    0 讨论(0)
  • 2020-11-29 10:28

    to fix this Remove @Id from Subclass

    in MicroTask keep

       @Id
        @GeneratedValue(generator = "system-uuid")
        @GenericGenerator(name = "system-uuid", strategy = "uuid")
        @Column(name = "MICROTASKID")
        private String microTaskId;
    

    in Subclass HumanMicroTask remove

       @Id
        @Column(name = "HMTID")
        private String humanMicroTaskid;
    
    0 讨论(0)
  • 2020-11-29 10:30

    I had the same problem some time ago, since your parent class has a primary key: 'Id', when the subclasses are generated they automatically generate a foreign key with the exact name of their parent's primary key

    Example: (Pseudocode)

    Entity Definition

    Parent Class

      @Entity
        @Inheritance(strategy = InheritanceType.JOINED)
        @Table(name = "abstract_person", catalog = "catalog", schema = "")
        class AbstractPerson{
    
            //Primary Key
            @Id
            @Column(name = "idPerson")
            int idPerson;
    
            @Basic
            @Column(name = "name")
            String name;
    
            //corresponding getters and setters
        }
    

    Child Class:

     @Entity
        @Table(name = "concrete_person", catalog = "catalog", schema = "")
        class ConcretePerson extends AbstractPerson{
    
           //No id or primary key is defined here
    
            @Basic
            @Column(name="profession")
            String profession;
    
        }
    

    Table Generation

    Parent class will map to this

    Table "abstract_person"
    id: Int (primary key)
    name: Varchar

    Child class will map to this:


    Table "concrete_person"
    profession: Varchar
    idPerson: int (Automatically generated, foreign key to parent table and primary class of this table)

    //Assumptions
    Mysql database;
    Jpa 2 Hibernate Implementation;
    NetBeans 7x Ide

    0 讨论(0)
  • 2020-11-29 10:34

    It is due to Id column in both classes. Remove the Id from HumanMicroTask.

    0 讨论(0)
提交回复
热议问题