不能使用< union-subclass>标识列密钥生成(TABLE_PER_CLASS) [英] Cannot use identity column key generation with <union-subclass> ( TABLE_PER_CLASS )

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

问题描述

com.something.SuperClass:

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

long confirmationCode;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)//导致异常!!!
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 ;

字符串名称;

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

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


code


给我这个例外:

 引起:org.hibernate.MappingException:无法使用identity列键
generation with< union-subclass> ;映射为:com.something.SuperClass

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

table-per-class继承和 GenerationType.Auto
考虑MsSQL中的标识列。它是基于列的。在每班级表策略中,每个班级使用一个表格,每个表格都有一个ID。



试试:

@GeneratedValue(strategy = GenerationType.TABLE)


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;
    }
}

Gives me this exception:

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

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

解决方案

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.

Try:

@GeneratedValue(strategy = GenerationType.TABLE)

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

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