Specifying a primary key on an @ElementCollection element

So, there is a behavior with innodb that can cause a problem if some tables do not have a primary key.

So, with Hibernate, I'm looking for a key to specify the primary key in the @ElementCollection table using Set as the understream data structure.

I found a way to have a primary key with a card, but this is rather strange because I do not need a card.

I also found an answer related to @Embeddable, but I don't need such complexity. I use Set or Set as a data structure in my objects.

Any idea how to achieve this?

+7
java orm hibernate jpa hibernate-mapping
source share
2 answers

If you use Set and make the Column element non-null, hibernate will make the primary key with the join column and the element column.

+8
source share

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

You can add @OrderColumn to optimize the generation of SQL statements.

If you need a primary key, you must include @ElementCollection in the @OneToMany association.

+2
source share

All Articles