Cannot use identity column key generation with ( TABLE_PER_CLASS )

后端 未结 6 2032
囚心锁ツ
囚心锁ツ 2020-12-02 07:12

com.something.SuperClass:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SuperClass implements Seria         


        
6条回答
  •  抹茶落季
    2020-12-02 07:52

    Agree with zoidbeck's answer. You need to change strategy to:

    @GeneratedValue(strategy = GenerationType.TABLE)
    

    But that's not all, you need to create a new table, what will hold your abstract's table primary key sequence. Modify your mapping to

    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "ConfirmationCodeGenerator")
    @TableGenerator(table = "SEQUENCES", name = "ConfirmationCodeGenerator")
    public long getConfirmationCode() {
       return confirmationCode;
    }
    

    And a new table in database should look like following: enter image description here

    When you ran your application, Hibernate will insert a row where sequence_name will be the entity name (SuperClass in this example) and sequence_next_hi_value value will be automatically incremented and used for new records of all implementing subclasses's tables.

提交回复
热议问题