Generated value for nonPK field

4.8k Views Asked by At

I have the entity of some BusinessParticipant, it just simple flat entity. And these Participants may be organized in groups, by user's wish. The group has no data, just id. So creating entity and table seems overkill... I'll wish to have 2 db tables, one for participants and one to link the participant to it's group. The problem is how should I generate the group id? All the GenerateValue & co. work with @id annotation only. Are there any way to mark the field that is not PK, to be automatically generated?

2

There are 2 best solutions below

0
On

Depends on your persistence provider. For example, ObjectDB supports @GeneratedValue on normal fields, but this is non-standard functionality. If you're using Hibernate, there's no clean way to do this, see this stackoverflow question: Hibernate JPA Sequence (non-Id)

0
On

I have created a proposal for JPA to support @GeneratedValue on non-id fields.

Please vote here for it to be included in a future release