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

前端 未结 4 793
一生所求
一生所求 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: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

提交回复
热议问题