现在的位置: 首页 > 综合 > 正文

5.3.6 复合主键@IdClass

2014年03月24日 ⁄ 综合 ⁄ 共 3289字 ⁄ 字号 评论关闭

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

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         必须覆盖equalshashCode方法。equals方法用于判断两个对象是否相同,EntityManger通过find方法来查找Entity时,是根据equals的返回值来判断的。本例中,只有对象的nameemail值完全相同时或同一个对象时则返回true,否则返回falsehashCode方法返回当前对象的哈希码,生成的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         在实体中同时标注主键的属性。本例中在emailnamegetter方法前标注@Id,表示符合主键使用这两个属性。

3)这样定义实体的复合主键后,通过以下代码便可以获得指定的实体对象:

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

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

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

抱歉!评论已关闭.