Specifying a primary key on @ElementCollection

為{幸葍}努か 提交于 2019-12-18 21:59:10

问题


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?


回答1:


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;



回答2:


@ElementCollection cannot take a primary key, because an Embeddable types cannot have an identifier.

You can add an @OrderColumn to optimize the generates SQL statements.

If you need a primary key, then you should turn the @ElementCollection into a @OneToMany association.



来源:https://stackoverflow.com/questions/30053647/specifying-a-primary-key-on-elementcollection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!