使用@Id和@EmbeddedId来区分复合键 [英] Difference using @Id and @EmbeddedId for a compound key

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

问题描述

我创建了一个使用@Id指向@Embeddable复合键的实体。我相信我所做的一切都很好。然而,将@Id切换到@EmbeddedId之后,就我所知,一切都可以继续正常工作。

之前:

  @Entity 
public final class MyEntity {
private CompoundKey id;

@Id
public CompoundKey getId(){
return id;
}

public void setId(CompoundKey id){
this.id = id;
}

后:

  @Entity 
public final class MyEntity {
private CompoundKey id;

@EmbeddedId
public CompoundKey getId(){
return id;
}

public void setId(CompoundKey id){
this.id = id;
}

使用@Id和@EmbeddedId注释引用复合键?

解决方案

实际上,根据规范,映射 Embeddable 复合键的正确方法是在<版本之后的。引用JPA 1.0规范:
$ b


2.1.4主键和实体标识



每个实体都必须有一个主键。



主键必须在作为实体
层次结构的根的
实体上定义或在
映射的超类上的实体层次结构。主键
必须在
实体层次结构中精确定义一次。



简单的(即非复合的)primary
键必须与
持久字段或
实体类的属性相对应。 Id 注释是用于表示简单主键的

请参阅9.1.8节。



复合主键必须
对应于单个
持久字段或属性,或者对应于
这样的字段或属性集合,如
所述。主键类
必须定义为表示
复合主键。当
数据库键由多个
列组成时,复合
主键通常在从旧数据库映射
时出现。 EmbeddedId 和and
IdClass 注释用于
表示复合主键。请参阅
小节9.1.14和9.1.15。

主键(或复合主键的字段或属性
)应该是
以下类型之一:任何Java
原始类型;任何原始包装器
类型; java.lang.String中;
java.util.Date ; java.sql.Date 。但是,在
通用中,不应在主键中使用近似数值的
类型(例如,浮点类型)

主键使用类型
以外的实体将不可移植。
如果使用生成的主键是
,则只有整型将是
。如果使用 java.util.Date 作为
a主键字段或属性,则
临时类型应指定为
DATE。


...

后来:


9.1.14 EmbeddedId注解



EmbeddedId 注释应用

的持久性字段或属性,实体类或映射超类
表示
是可嵌入类的复合主键。嵌入式
类必须注解为
嵌入式



必须只有一个当
使用 EmbeddedId 注释时, EmbeddedId
注释并且没有Id注释。



I've created an entity that uses @Id to point to an @Embeddable compound key. Everything I believe works fine as is. However, after switching @Id to @EmbeddedId everything continues to work fine as far as I can tell.

Before:

@Entity
public final class MyEntity {
    private CompoundKey id;

    @Id
    public CompoundKey getId() {
        return id;
    }

    public void setId(CompoundKey id) {
        this.id = id;
    }

After:

@Entity
public final class MyEntity {
    private CompoundKey id;

    @EmbeddedId
    public CompoundKey getId() {
        return id;
    }

    public void setId(CompoundKey id) {
        this.id = id;
    }

Is there a difference between using the @Id and @EmbeddedId annotations when referencing a compound key?

解决方案

I'm actually surprised the "before" version is working. According to the specification, the correct way to map your Embeddable compound key is the "after" version. Quoting the JPA 1.0 specification:

2.1.4 Primary Keys and Entity Identity

Every entity must have a primary key.

The primary key must be defined on the entity that is the root of the entity hierarchy or on a mapped superclass of the entity hierarchy. The primary key must be defined exactly once in an entity hierarchy.

A simple (i.e., non-composite) primary key must correspond to a single persistent field or property of the entity class. The Id annotation is used to denote a simple primary key. See section 9.1.8.

A composite primary key must correspond to either a single persistent field or property or to a set of such fields or properties as described below. A primary key class must be defined to represent a composite primary key. Composite primary keys typically arise when mapping from legacy databases when the database key is comprised of several columns. The EmbeddedId and and IdClass annotations are used to denote composite primary keys. See sections 9.1.14 and 9.1.15.

The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date. In general, however, approximate numeric types (e.g., floating point types) should never be used in primary keys. Entities whose primary keys use types other than these will not be portable. If generated primary keys are used, only integral types will be portable. If java.util.Date is used as a primary key field or property, the temporal type should be specified as DATE.

...

And later:

9.1.14 EmbeddedId Annotation

The EmbeddedId annotation is applied to a persistent field or property of an entity class or mapped superclass to denote a composite primary key that is an embeddable class. The embeddable class must be annotated as Embeddable.

There must be only one EmbeddedId annotation and no Id annotation when the EmbeddedId annotation is used.

这篇关于使用@Id和@EmbeddedId来区分复合键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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