不能使用带有 <union-subclass> 的标识列键生成.( TABLE_PER_CLASS ) [英] Cannot use identity column key generation with <union-subclass> ( TABLE_PER_CLASS )

查看:23
本文介绍了不能使用带有 <union-subclass> 的标识列键生成.( TABLE_PER_CLASS )的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 的最佳和最方便的方法是什么?我不想改变我的继承策略.

What's the best and most convenient way for me to generate the ID's? I do not want to change my inheritance strategy.

推荐答案

这里的问题是你混合了table-per-class"继承和 GenerationType.Auto.考虑 MsSQL 中的标识列.它是基于列的.在每班一张桌子"的策略中,您每班使用一张桌子,每张桌子都有一个 ID.

The problem here is that you mix "table-per-class" inheritance and GenerationType.Auto. Consider an identity column in MsSQL. It is column based. In a "table-per-class" strategy you use one table per class and each one has an ID.

试试:

@GeneratedValue(strategy = GenerationType.TABLE)

这篇关于不能使用带有 &lt;union-subclass&gt; 的标识列键生成.( TABLE_PER_CLASS )的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆