hibernate复合主键方式(2)

来源:互联网 发布:淘宝海运价格表 编辑:程序博客网 时间:2024/06/05 08:21

方式二:将主键属性提取到一个主键类中,实体类只需包含主键类的一个引用。
   
    主键类:
   
    /*必须实现Serializable接口*/
   
    public class PeoplePrimaryKey implements Serializable
   
    {
   
    private static final long serialVersionUID = -1190986010439330142L;
   
    /*复合主键值*/
   
    private String id;
   
    private String name;
   
    public PeoplePrimaryKey()
   
    {
   
    }
   
    /*复合主键值的get和set方法*/
   
    public String getId()
   
    {
   
    return id;
   
    }
   
    public void setId(String id)
   
    {
   
    this.id = id;
   
    }
   
    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 + ((id == null) ? 0 : id.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;
   
    PeoplePrimaryKey other = (PeoplePrimaryKey) obj;
   
    if (id == null)
   
    {
   
    if (other.id != null)
   
    return false;
   
    }
   
    else if (!id.equals(other.id))
   
    return false;
   
    if (name == null)
   
    {
   
    if (other.name != null)
   
    return false;
   
    }
   
    else if (!name.equals(other.name))
   
    return false;
   
    return true;
   
    }
   
    }

实体类:
   
    public class People  {
   
    /*持有主键类的一个引用,使用该引用作为这个类的OID*/
   
    private PeoplePrimaryKey peoplePrimaryKey;
   
    private int age;
   
    public People()
   
    {
   
    }
   
    public PeoplePrimaryKey getPeoplePrimaryKey()
   
    {
   
    return peoplePrimaryKey;
   
    }
   
    public void setPeoplePrimaryKey(PeoplePrimaryKey peoplePrimaryKey)
   
    {
   
    this.peoplePrimaryKey = peoplePrimaryKey;
   
    }
   
    public int getAge()
   
    {
   
    return age;
   
    }
   
    public void setAge(int age)
   
    {
   
    this.age = age;
   
    }
   
    }
   
    People.hbm.xml文件稍有一点变动:
   
    <?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">
   
    <hibernate-mapping>
   
    <class name="com.suxiaolei.hibernate.pojos.People" table="people">
   
    <!-- 复合主键使用composite-id标签 -->
   
    <!--          name - 指定了复合主键对应哪一个属性
   
    class - 指定了复合主键属性的类型          -->
   
    <composite-id name="peoplePrimaryKey" class="com.suxiaolei.hibernate.pojos.PeoplePrimaryKey">
   
    <!-- key-property指定了复合主键由哪些属性组成 -->
   
    <key-property name="id" column="id" type="string"></key-property>
   
    <key-property name="name" column="name" type="string"></key-property>
   
    </composite-id>
   
    <property name="age" column="age" type="integer"></property>
   
    </class> </hibernate-mapping>
   
    场景测试与方式一大同小异这里不再举例了。主键类为什么实现Serializable接口和为什么重写equals和hashCode方法上面已经解释的很清楚了。