JPA2 OneToMany without primary key on the ‘Many’ side

JPA entities must have a primary key. However, there are cases where a primary key is superfluous. Instead of defining a useless primary key column just to satisfy an object model, JPA2 offers the possibility to use collections of embeddable objects.

If we imagine the following table relation (PostgreSQL), one without primary key for the second table:

 CREATE TABLE translatables (
    code VARCHAR(80) NOT NULL,
    category VARCHAR(20) NOT NULL);

 CREATE TABLE translatables_translations (
    translatable_id BIGINT NOT NULL,
    lang_code VARCHAR(2) NOT NULL,
    text VARCHAR(1024),
    UNIQUE(translatable_id, lang_code),
    CONSTRAINT translatables_fk FOREIGN KEY(translatable_id)
                                    REFERENCES translatables(id));

Note: we could replace the UNIQUE constraint by a primary key, but it’s not the right solution because it is simply not the natural primary key.

We can use the following JPA2 construct to represent this in our application:

 public class Translatable implements Serializable {

   @GeneratedValue(/* use your favorite generator here */)
   private Long id;
   private String code;
   private String category;

   private List translations;

 public class Translation implements Serializable {

   private Long translatableId;

   private String langCode;

   private String text;

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.