一尘不染

无法将身份列密钥生成用于 (TABLE_PER_CLASS)

hibernate

com.something.SuperClass:

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class SuperClass implements Serializable {
    private static final long serialVersionUID = -695503064509648117L;

    long confirmationCode;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO) // Causes exception!!!
    public long getConfirmationCode() {
        return confirmationCode;
    }

    public void setConfirmationCode(long confirmationCode) {
        this.confirmationCode = confirmationCode;
    }
}

com.something.SubClass:

@Entity
public abstract class Subclass extends SuperClass {
    private static final long serialVersionUID = 8623159397061057722L;

    String name;

    @Column(nullable = false)
    public String getName() {
        return name;
    }

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

给我这个例外:

Caused by: org.hibernate.MappingException: Cannot use identity column key
generation with <union-subclass> mapping for: com.something.SuperClass

对我来说,生成ID的最好和最方便的方法是什么? 我不想更改继承策略。


阅读 282

收藏
2020-06-20

共1个答案

一尘不染

这里的问题是您将“每类表”继承与混合在一起GenerationType.Auto。考虑MsSQL中的标识列。它基于列。在“每类表”策略中,每个类使用一个表,每个表都有一个ID。

尝试:

@GeneratedValue(strategy = GenerationType.TABLE)

2020-06-20