Schema change for unidirectional OneToMany assocation when migrating to Hibernate 5

Solution Verified - Updated -

Issue

  • A unidirectional OneToMany is defined where the 'one' class has a table name which differs from the class name

    @Entity
    @Table(name="Person")
    public class Employee {
            @Id
            private long id;
    
            @OneToMany
            private java.util.Set<Task> tasks;
    }
    
    @Entity
    public class Task {
            @Id
            private long id;
    }
    
  • After migration from EAP 6 / Hibernate 4 to EAP 7 / Hibernate 5, Hibernate expects a different primary key name in the link table for the 'one' entity

    -- EAP 6 / Hibernate 4 generated DDL for link table
    create table Person_Task (
        Person_id bigint not null, -- 'one' entity table name
        tasks_id bigint not null,
        primary key (Person_id, tasks_id)
    )
    
    -- EAP 7 / Hibernate 5 generated DDL for link table
    create table Person_Task (
        Employee_id bigint not null, -- 'one' entity name rather than table name
        tasks_id bigint not null,
        primary key (Employee_id, tasks_id)
    )
    
  • Depending on the hibernate.hbm2ddl.auto setting, errors such as the below are reported and validation fails

    ... org.hibernate.tool.schema.spi.SchemaManagementException: Schema-validation: missing column [Employee_id] in table [Person_Task]
    
    ... ORA-00904: "Employee_id": invalid identifier
    
    ... ORA-01758: table must be empty to add mandatory (NOT NULL) column
    

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In