JPA Composite key (all fields are notNull and PRI)
-
20-09-2019 - |
Question
I saw that exist more then one way to map a Composite Key with JPA.
But in my case is kind of different:
I have a table with only 2 column:
mysql> desc mytable; +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------+-------------+------+-----+---------+-------+ | name | varchar(80) | NO | PRI | | | | tag | varchar(80) | NO | PRI | | | +--------+-------------+------+-----+---------+-------+
My point is: Do I need to create a new (primary key class) class just to map my composite key?
I'm trying to find the easiest way to do it.
Some one can help-me?
Thanks in advance!
I'm trying with this approach: http://www.java.net/print/236710
Solution
Do I need to create a new (primary key class) class just to map my composite key?
To my knowledge, yes, you need to.
I'm trying to find the easiest way to do it.
The tutorial How to use Compound Primary Keys with Hibernate and JPA Annotations does a great job at summarizing the various options (@Id
vs @IdClass
vs @EmbeddedId
) and, in my opinon, the latter is the easiest way (less verbose).