Hibernate的学习总结(复合主键)

    技术2025-08-08  10

    当表存在复合主键时,Hibernate对其的处理稍微复杂一些。

    1.POJO类得实现Serializable接口

    2.POJO类得重写equals()方法和hashCode()方法

    3.hbm文件中用<composite-id>节点配置复合主键信息

     

    其中重写equals()方法和hashCode()方法可以使用commons-lang包来简化处理

     

    以下是一个小例子:

    import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Person implements Serializable { private String name; private String phone; private int age; public boolean equals(Object obj) { // TODO 自动生成方法存根 if (this == obj) { return true; } if (!(obj instanceof Person)) { return false; } Person p = (Person) obj; return new EqualsBuilder().append(this.name, p.name).append(this.phone, p.phone).append(this.age, p.age).isEquals(); } public int hashCode() { // TODO 自动生成方法存根 return new HashCodeBuilder().append(this.name).append(this.age).append( this.phone).toHashCode(); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

     

    其hbm配置文件代码为:

    <?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Sat Nov 11 21:30:08 CST 2006 --> <hibernate-mapping package="org.lxh.hibernate02"> <class name="Person" table="PERSON"> <composite-id> <key-property name="name" column="NAME" type="string"/> <key-property name="phone" column="PHONE" type="string"/> </composite-id> <property name="age" column="AGE" type="int" /> </class> </hibernate-mapping>

     

    还有一种更好的方式是,将复合主键部分单独提出一个POJO类

    person类

    import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Person implements Serializable { private PersonKey id ; private int age; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public PersonKey getId() { return id; } public void setId(PersonKey id) { this.id = id; } }

     

    PersonKey类

    /* * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. * * Created Sat Nov 11 21:36:22 CST 2006 by MyEclipse Hibernate Tool. */ package org.lxh.hibernate03; import java.io.Serializable; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; /** * A class representing a composite primary key id for the PERSON * table. This object should only be instantiated for use with instances * of the Person class. * WARNING: DO NOT EDIT THIS FILE. This is a generated file that is synchronized * by MyEclipse Hibernate tool integration. */ public class PersonKey implements Serializable { /** The cached hash code value for this instance. Settting to 0 triggers re-calculation. */ private volatile int hashValue = 0; /** The value of the NAME component of this composite id. */ private java.lang.String name; /** The value of the PHONE component of this composite id. */ private java.lang.String phone; /** * Simple constructor of PersonKey instances. */ public PersonKey() { } /** * Returns the value of the name property. * @return java.lang.String */ public java.lang.String getName() { return name; } /** * Sets the value of the name property. * @param name */ public void setName(java.lang.String name) { hashValue = 0; this.name = name; } /** * Returns the value of the phone property. * @return java.lang.String */ public java.lang.String getPhone() { return phone; } /** * Sets the value of the phone property. * @param phone */ public void setPhone(java.lang.String phone) { hashValue = 0; this.phone = phone; } /** * Implementation of the equals comparison on the basis of equality of the id components. * @param rhs * @return boolean */ public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof Person)) { return false; } PersonKey p = (PersonKey) obj; return new EqualsBuilder().append(this.name, p.getName()).append(this.phone, p.getPhone()).isEquals(); } /** * Implementation of the hashCode method conforming to the Bloch pattern with * the exception of array properties (these are very unlikely primary key types). * @return int */ public int hashCode() { return new HashCodeBuilder().append(this.name).append( this.phone).toHashCode(); } }

     

    hbm文件

    <?xml version="1.0" encoding='UTF-8'?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" > <!-- DO NOT EDIT: This is a generated file that is synchronized --> <!-- by MyEclipse Hibernate tool integration. --> <!-- Created Sat Nov 11 21:36:21 CST 2006 --> <hibernate-mapping package="org.lxh.hibernate03"> <class name="Person" table="PERSON"> <composite-id name="id" class="PersonKey"> <!-- 加上复合主键类的类名 --> <key-property name="name" column="NAME" type="string"/> <key-property name="phone" column="PHONE" type="string"/> </composite-id> <property name="age" column="AGE" type="int" /> </class> </hibernate-mapping>

     

    最新回复(0)