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
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
Author

Composite key in collection of basic type

Darko Ristic
Greenhorn

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
Greenhorn

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
Bartender

Joined: Nov 09, 2011
Posts: 1030
    
    5

Hi

You can use composite keys with JPA. An example is shown here.

http://weblogs.java.net/blog/bleonard/archive/2006/11/using_composite.html
 
jQuery in Action, 2nd edition
 
subject: Composite key in collection of basic type