JPA复合主键 [英] JPA composite primary key

查看:236
本文介绍了JPA复合主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的JPA模型中有以下类(getters,setters和不相关的字段被忽略):

$ p $ @Entity @继承(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 )
私人整数ID;
}

我需要定义一个类 Price ,这样当 DDL是从类,相应表的主键由 Product Currency 的键组成。我试过以下内容:

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

@Id @ManyToOne(可选= false)
private产品产品;

@Id
@ManyToOne(可选= false)
专用货币货币;
}

@Embeddable
公共类PricePK实现Serializable {

整数产品;
整数货币;
}

但是这会为 PRICE code> table:

  create table PRICE(
currency_id int null,
product_id int null ,
主键(currency_id,product_id)
);

请注意, currency_id product_id 是可空的,当我尝试将DDL加载到SQL Server中时导致以下错误


无法在表'PRICE'中为可空列定义PRIMARY KEY约束


我不明白为什么这些是可以为空的,因为在它们被注释为
@ManyToOne(可选= false)



DDL是使用 org.hibernate.dialect.SQLServerDialect SQL方言。

解决方案

使用复合主键和注释作为双向 @OneToMany 创建ManyToMany关系。此代码完美无瑕。也许它会有帮助:



使用复合主键和注释映射ManyToMany:


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.

解决方案

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:

这篇关于JPA复合主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆