在@ElementCollection上指定主键 [英] Specifying a primary key on @ElementCollection

查看:97
本文介绍了在@ElementCollection上指定主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,有了Hibernate,我正在寻找一个密钥来指定一个@ElementCollection表上的一个主键,一个Set作为下层数据结构。

我找到了一个有主键和地图的方法,但它是一种奇怪,因为我不需要地图。



我也找到了一个与@Embeddable有关的答案,但我不需要那种复杂性。我在实体中使用Set或Set作为数据结构。



任何想法如何实现?

解决方案

如果您使用Set并使元素Column不为null,则hibernate将使用连接列和元素列创建主键。

示例:

  @Column(name =STRINGS,nullable = false)
@ ElementCollection
private Set< String>串;


So, there is that behavior with innodb that can cause problem if some tables lack of primary key.

So with Hibernate, I am looking for a key to specifies a primary key on a @ElementCollection table with a Set as the underling data structure.

I found a way to have a primary key with a map, but it is kind of weird because I do not need a map.

I also found an answer related to @Embeddable, but I do not need that kind of complexities. I am using a Set or Set as the data structure in my entities.

Any idea how to achieve that?

解决方案

If you use a Set and make the element Column be not null, then hibernate will make a primary key with the join column and element column.

Example:

@Column(name = "STRINGS", nullable = false)
@ElementCollection
private Set<String> strings;

这篇关于在@ElementCollection上指定主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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