无法使用生成标识列键 (表_每_类)
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
对我来说生成身份证? 我不想改变我的继承策略。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里的问题是你混合了“每类一个表”继承和
GenerationType.Auto
。考虑 MsSQL 中的标识列。 它是基于列的。 在“每类一张表”策略中,每个类使用一个表,并且每个表都有一个 ID。
尝试:
@GenerateValue(strategy = GenerationType.TABLE)
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)
我想知道这是否是一个数据库方言特定的问题,因为在观看以 PostgreSQL 作为底层数据库的 YouTube 教程时,我看到视频的创建者成功运行了一个具有默认 @GenerateValue 的应用程序。 就我而言(底层数据库是 MySQL),我必须将 @GenerateValue 策略修改为 GenerationType.TABLE,完全按照 zoidbeck 的建议。
视频如下:https://www.youtube.com/watch?v=qIdM4KQOtH8
I wonder if this is a database dialect specific problem, since watching a youtube tutorial with PostgreSQL as the underlying database I saw that the creator of the video run succefully an app with the default @GeneratedValue. In my case (the underlying database is MySQL) I had to modify the @GeneratedValue strategy to GenerationType.TABLE exactly as zoidbeck proposes.
Here is the video : https://www.youtube.com/watch?v=qIdM4KQOtH8
同意zoidbeck的回答。
您需要将策略更改为:
但这还不是全部,您需要创建一个新表,该表将保存摘要的表主键序列。 将映射修改为
数据库中的新表应如下所示:
当您运行应用程序时,Hibernate 将在
sequence_name
所在位置插入一行实体名称(本例中为SuperClass
)和sequence_next_hi_value
值将自动递增并用于所有实现子类表的新记录。Agree with zoidbeck's answer.
You need to change strategy to:
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
And a new table in database should look like following:
When you ran your application, Hibernate will insert a row where
sequence_name
will be the entity name (SuperClass
in this example) andsequence_next_hi_value
value will be automatically incremented and used for new records of all implementing subclasses's tables.您可以使用@MappedSuperclass进行继承
you can use @MappedSuperclass for inheritance
在我们的例子中,我们使用 PostreSQL 数据库进行开发和生产,并使用内存 hsqldb 数据库进行测试。 我们在这两种情况下都使用序列来生成 id。 显然,对于 postgres,
GenerationType.AUTO
默认为SEQUENCE
,但在我们的本地测试中失败了(对于 hsqldb 必须默认为其他值)。因此,对我们有用的解决方案是显式使用
GenerationType.SEQUENCE
。In our case, we use a PostreSQL database for dev and production and an in-memory hsqldb database for tests. We are using a sequence in both cases to generate an id. Apparently
GenerationType.AUTO
defaults toSEQUENCE
for postgres, but failed in our local tests (must default to something else for hsqldb).So the solution that worked for us, explicitly use
GenerationType.SEQUENCE
.