• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA 2: composite primary key classes

 
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Mike and Merrick.

Would you please provide a couple examples of primary key classes using composite keys? The JPA 2 spec has a lot of rules regarding composite primary keys, and it would be helpful to me to see some basic examples.

Thanks in advance.
 
Rob Ivan
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Those are very clear--especially the java2s.com tutorial.
 
author
Posts: 304
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,

Not sure if you wanted JPA 2 examples of composite PKs or just regular JPA examples. The JPA 2 spec goes quite a lot further in composite primary keys, and it gets quite hairy when you follow the path all the way to its inner dark corners. None of the examples in the earlier post actually showed any JPA 2 examples, they were just JPA 1.0 examples. If you are interested in a couple of simple examples of JPA 2, the most common case is that of having a @ManyToOne relationship as part of an entity PK.

So we have a CompactDisc that has a title and a relationship to an Artist. The key is on both the title and the artist.

@Entity @IdClass(CompactDiscPK.class)
public class CompactDisc {
@Id String title;
@Id @ManyToOne Artist artist;
//...
}

public class CompactDiscPK {
public String title;
public int artist;
// ...
}

@Entity
public class Artist {
@Id int id;
// ...
}

Note that we can now use the @Id annotation on the many-to-one relationship. The PK class has attributes that are named the same as those on the CompactDisc class, but in the case of the "artist" field, the attribute of the PK class is not of type Artist, but of type int, which is the type of the PK of Artist. This is much easier to map than it used to be in JPA 1.0, when you had to create a second redundant field in CompactDisc and map it as read-only.

The second example is when you want to map the PK in CompactDisc as an embedded id:

@Entity
public class CompactDisc {
@EmbeddedId CompactDiscPK id;
@ManyToOne @MapsId Artist artist;
//...
}

@Embedded
public class CompactDiscPK {
public String title;
public int artist;
// ...
}


In this example the "artist" field in the embeddable CompactDiscPK class is actually being mapped by the ManyToOne artist field in CompactDisc. The new @MapsId annotation allows this to happen. The default join column (ARTIST_ID) will apply in both examples. Neat, eh?
 
Rob Ivan
Greenhorn
Posts: 29
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is more what I had in mind. Very neat. Thank you.
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following page discusses
composite primary keys
http://www.relationaldbdesign.com/relational-database-analysis/module2/composite-primary-keys.php
within the context of relational database design.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic