aspose file tools*
The moose likes Object Relational Mapping and the fly likes Composite key in collection of basic type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Composite key in collection of basic type" Watch "Composite key in collection of basic type" New topic

Composite key in collection of basic type

Darko Ristic

Joined: Feb 23, 2012
Posts: 6
Hi all...
I have collection of values of basic type String, and want to have composite key in collection table.
Can anyone help with this in order how to make composite key in PERSON_EMAIL_ADDRESS table, consisting of PERSON_ID and EMAIL_ADDRESS?

This is entity with annotations:
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
I assume you are talking about DDL generation?

The CollectionTable annotation contains a uniqueConstraints attribute that you could use to define a unique constraint on these columns.
JPA does not provide a way to set the primary key constraint, other than the @Id annotation on an Entity. So, you could map Email as an entity instead of element collection.

You could also just define the table using your own DDL scripts.

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Darko Ristic

Joined: Feb 23, 2012
Posts: 6
But what if I say something like:

I'v checked table informations and it says EMAIL_ADDRESS is pk.
Beside, PERSON_EMAIL_ADDRESS is also pk.
Since I'm working with mySQL database, wonder if this is just case with mySQL or we could say it is the rule?
James Boswell

Joined: Nov 09, 2011
Posts: 973


You can use composite keys with JPA. An example is shown here.
I agree. Here's the link:
subject: Composite key in collection of basic type
Similar Threads
Define OneToMany table column as part of composite key (primary-key class) query
removing a child
Collection of values question
How to query a value typed object
Hibernate Collections - have lots of null elements aswell as my objects