如何使用 JPA 和 Hibernate 映射复合键? [英] How to map a composite key with JPA and Hibernate?

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

问题描述

这段代码中,如何为组合键生成Java类(hibernate中如何组合键):

In this code, how to generate a Java class for the composite key (how to composite key in hibernate):

create table Time (
     levelStation int(15) not null,
     src varchar(100) not null,
     dst varchar(100) not null,
     distance int(15) not null,
     price int(15) not null,
     confPathID int(15) not null,
     constraint ConfPath_fk foreign key(confPathID) references ConfPath(confPathID),
     primary key (levelStation, confPathID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

推荐答案

要映射复合键,可以使用 EmbeddedId IdClass注释.我知道这个问题严格来说并不是关于 JPA,但规范定义的规则也适用.所以他们在这里:

To map a composite key, you can use the EmbeddedId or the IdClass annotations. I know this question is not strictly about JPA but the rules defined by the specification also applies. So here they are:

...

复合主键必须对应于单个持久字段或属性或一组这样的字段或属性,如如下面所描述的.一个主键类必须定义为代表一个复合主键.合成的主键通常出现在来自遗留数据库的映射,当数据库密钥由几个列.EmbeddedIdIdClass 注释用于表示复合主键.参见第 9.1.14 和 9.1.15 节.

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 IdClass annotations are used to denote composite primary keys. See sections 9.1.14 and 9.1.15.

...

以下规则适用于复合主键:

The following rules apply for composite primary keys:

  • 主键类必须是公共的,并且必须有一个公共的无参数构造函数.
  • 如果使用基于属性的访问,主键的属性类必须是公共的或受保护的.
  • 主键类必须可序列化.
  • 主键类必须定义 equalshashCode方法. 值的语义这些方法的相等性必须是与数据库等式一致对于数据库类型键已映射.
  • 复合主键必须表示和映射为可嵌入类(参见第 9.1.14 节,EmbeddedId Annotation")或必须是表示并映射到多个实体的字段或属性类(参见第 9.1.15 节,IdClass注释").
  • 如果复合主键类映射到多个字段或实体类的属性,主键字段的名称或主键类中的属性实体类的必须对应,它们的类型必须是

使用 IdClass

复合主键的类可能看起来像(可能是静态内部类):

With an IdClass

The class for the composite primary key could look like (could be a static inner class):

public class TimePK implements Serializable {
    protected Integer levelStation;
    protected Integer confPathID;

    public TimePK() {}

    public TimePK(Integer levelStation, Integer confPathID) {
        this.levelStation = levelStation;
        this.confPathID = confPathID;
    }
    // equals, hashCode
}

和实体:

@Entity
@IdClass(TimePK.class)
class Time implements Serializable {
    @Id
    private Integer levelStation;
    @Id
    private Integer confPathID;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;

    // getters, setters
}

IdClass 注释将多个字段映射到表 PK.

The IdClass annotation maps multiple fields to the table PK.

复合主键的类可能看起来像(可能是静态内部类):

The class for the composite primary key could look like (could be a static inner class):

@Embeddable
public class TimePK implements Serializable {
    protected Integer levelStation;
    protected Integer confPathID;

    public TimePK() {}

    public TimePK(Integer levelStation, Integer confPathID) {
        this.levelStation = levelStation;
        this.confPathID = confPathID;
    }
    // equals, hashCode
}

和实体:

@Entity
class Time implements Serializable {
    @EmbeddedId
    private TimePK timePK;

    private String src;
    private String dst;
    private Integer distance;
    private Integer price;

    //...
}

@EmbeddedId 注释将 PK 类映射到表 PK.

The @EmbeddedId annotation maps a PK class to table PK.

  • 从物理模型的角度来看,没有区别
  • @EmbeddedId 以某种方式更清楚地传达了密钥是复合密钥并且 IMO 有意义当组合的 pk 本身是一个有意义的实体或它在您的代码中重用时.
  • @IdClass 用于指定某些字段组合是唯一的,但这些组合没有特殊含义.
  • From the physical model point of view, there are no differences
  • @EmbeddedId somehow communicates more clearly that the key is a composite key and IMO makes sense when the combined pk is either a meaningful entity itself or it reused in your code.
  • @IdClass is useful to specify that some combination of fields is unique but these do not have a special meaning.

它们还会影响您编写查询的方式(使它们或多或少冗长):

They also affect the way you write queries (making them more or less verbose):

  • IdClass

select t.levelStation from Time t

  • 带有 EmbeddedId

    select t.timePK.levelStation from Time t
    

    • JPA 1.0 规范
      • 第 2.1.4 节主键和实体身份"
      • 第 9.1.14 节EmbeddedId 注释"
      • 第 9.1.15 节IdClass 注释"

      这篇关于如何使用 JPA 和 Hibernate 映射复合键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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