---
title: "复合主键@IdClass "
author: "liyiye"
date: "2009-10-21T07:46:34Z"
tags: []
slug: "idclass"
draft: false
lastmod: "2009-10-21T07:46:34Z"
id: 497364
url: "https://liyiye.iteye.com/blog/497364"
---

有时一个实体的主键可能同时为多个，例如同样是之前使用的“ CustomerEO ”实体，需要通过 name 和 email 来查找指定实体，当且仅当 name 和 email 的值完全相同时，才认为是相同的实体对象。要配置这样的复合主键，步骤如以下所示。

（ 1 ）编写一个复合主键的类 CustomerPK ，代码如下。

CustomerPK.java

import java.io.Serializable;

 

public class CustomerPK **implements Serializable** {

 

         **public CustomerPK() {**

 

         **}**

 

         public CustomerPK(String name, String email) {

                   this.name = name;

                   this.email = email;

         }

 

         private String email;

 

         public String getEmail() {

                   return email;

         }

 

         public void setEmail(String email) {

                   this.email = email;

         }

 

         private String name;

 

         public String getName() {

                   return name;

         }

 

         public void setName(String name) {

                   this.name = name;

         }

 

         **@Override**

         **public int hashCode() {**

                   **final int PRIME = 31;**

                   **int result = 1;**

                   **result = PRIME * result + ((email == null) ? 0 : email.hashCode());**

                   **result = PRIME * result + ((name == null) ? 0 : name.hashCode());**

                   **return result;**

         **}**

 

         **@Override**

         **public boolean equals(Object obj) {**

                   **if (this == obj)**

                            **return true;**

                   **if (obj == null)**

                            **return false;**

                   **if (getClass() != obj.getClass())**

                            **return false;**

                   **final CustomerPK other = (CustomerPK) obj;**

                   **if (email == null) {**

                            **if (other.email != null)**

                                     **return false;**

                   **} else if (!email.equals(other.email))**

                            **return false;**

                   **if (name == null) {**

                            **if (other.name != null)**

                                     **return false;**

                   **} else if (!name.equals(other.name))**

                            **return false;**

                   **return true;**

         **}**

 

}

作为符合主键类，要满足以下几点要求。

l          必须实现 Serializable 接口。

l          必须有默认的 public 无参数的构造方法。

l          必须覆盖 equals 和 hashCode 方法。 equals 方法用于判断两个对象是否相同， EntityManger 通过 find 方法来查找 Entity 时，是根据 equals 的返回值来判断的。本例中，只有对象的 name 和 email 值完全相同时或同一个对象时则返回 true ，否则返回 false 。 hashCode 方法返回当前对象的哈希码，生成的 hashCode 相同的概率越小越好，算法可以进行优化。

（ 2 ）通过 @IdClass 注释在实体中标注复合主键，实体代码如下。

@Entity

@Table(name = "customer")

**@IdClass(CustomerPK.class)**

public class CustomerEO implements java.io.Serializable {

 

         private Integer id;

 

         public Integer getId() {

                   return this.id;

         }

 

         public void setId(Integer id) {

                   this.id = id;

         }

        

         private String name;

 

**@Id**

         public String getName() {

                   return this.name;

         }

 

         public void setName(String name) {

                   this.name = name;

         }

        

         private String email;

**@Id**

         public String getEmail() {

                   return email;

         }

 

         public void setEmail(String email) {

                   this.email = email;

         }

}

标注复合主键时需要注意以下几个问题。

l          @IdClass 标注用于标注实体所使用主键规则的类。它的定义如下所示。

@Target({TYPE}) @Retention(RUNTIME)

public @interface IdClass {

Class value();

}

属性 Class 表示符合主键所使用的类，本例中使用 CustomerPK 这个复合主键类。

l          在实体中同时标注主键的属性。本例中在 email 和 name 的 getter 方法前标注 @Id ，表示符合主键使用这两个属性。

（ 3 ）这样定义实体的复合主键后，通过以下代码便可以获得指定的实体对象：

    CustomerPK cpk = new CustomerPK("Janet","janetvsfei@yahoo.com.cn");

    CustomerEO instance = entityManager.find(CustomerEO.class, cpk);

提示：符合主键的值一般要通过程序设置，而不是按照某一个规则自动生成的。