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


Was it helpful?

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top