plcstpierre plcstpierre -4 years ago 72
Java Question

Specifying a primary key on @ElementCollection

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?

Answer Source

@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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download