JPA复合主键

编程入门 行业动态 更新时间:2024-10-28 10:30:47
本文介绍了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复合主键

本文发布于:2023-10-22 23:26:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519039.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:主键   JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!