Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA composite primary key [duplicate]

I have the following classes in my JPA model (getters, setters, and irrelevant fields omitted):

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Currency {

    @Id
    private Integer ix;
}

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Product {

    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;
}

I need to define a class Price, such that when the DDL is generated from the classes, the primary key of the corresponding table is composed of the keys for Product and Currency. I've tried the following:

@Entity @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@IdClass(PricePK.class)
public class Price {

    @Id @ManyToOne(optional = false)
    private Product product;

    @Id
    @ManyToOne(optional = false)
    private Currency currency;
}

@Embeddable
public class PricePK implements Serializable {

    Integer product;        
    Integer currency;
}

But this generates the following for the PRICE table:

create table PRICE (
    currency_id int null,
    product_id int null,
    primary key (currency_id, product_id)
);

Notice that both currency_id and product_id are nullable, which causes the following error when I try to load the DDL into SQL Server

Cannot define PRIMARY KEY constraint on nullable column in table 'PRICE'

I don't understand why these are nullable, because in the domain model they are annotated @ManyToOne(optional = false)

The DDL is generated using the org.hibernate.dialect.SQLServerDialect SQL dialect.

like image 223
Dónal Avatar asked Jun 23 '11 07:06

Dónal


People also ask

Can composite primary key have duplicate values?

Composite primary key rules Doesn't allow null values and cannot contain duplicates. Values in an individual column can be duplicated, but across the columns they must be unique. Null values are not allowed in any columns in the composite primary key.

Does a composite primary key have to be unique?

A composite key is made by the combination of two or more columns in a table that can be used to uniquely identify each row in the table when the columns are combined uniqueness of a row is guaranteed, but when it is taken individually it does not guarantee uniqueness, or it can also be understood as a primary key made ...

How does JPA implement composite primary key?

In JPA, we have two options to define the composite keys: the @IdClass and @EmbeddedId annotations. In order to define the composite primary keys, we should follow some rules: The composite primary key class must be public. It must have a no-arg constructor.

Does JPA require primary key?

Every JPA entity must have a primary key. You can specify a primary key as a single primitive, or JDK object type entity field (see "Configuring a JPA Entity Simple Primary Key Field").


1 Answers

Recently I created ManyToMany relation using Composite Primary key and annotation as bi directional @OneToMany. This code works flawless. Maybe it will help:

Mapping ManyToMany with composite Primary key and Annotation:

like image 95
danny.lesnik Avatar answered Sep 29 '22 22:09

danny.lesnik