《Hibernate学习笔记三》:联合主键的映射

本文详细介绍了使用Hibernate进行联合主键映射的两种方式:通过XML映射文件和使用Annotation。包括实体类、联合主键类、映射文件、配置文件和测试类的编写与解析,强调了实体类需要实现Serializable接口并重写equals和hashCode方法。此外,还对比了使用@Embeddable、@EmbeddedId和@IdClass注解的不同实现方法。
摘要由CSDN通过智能技术生成

《Hibernate学习笔记三》:联合主键的映射

就如在前面所举的例子一样,是使用的id作为唯一的主键,一般情况下我们也只使用唯一的一个属性作为主键,但是在实际中,我们可能会遇到几个属性作为主键的情况,因此,在本篇博文中,就来介绍下,联合主键的映射关系应该如何来做??

联合主键的映射有两种方式来进行实现。

1、使用映射文件 XXX.hbm.xml

2、使用Annotation

Hibernate首先需要使用联合主键的实体类必须实现Serializable接口,即为了使序列能够被序列化进行传输,并要求实体类重写equals、hashCode这两个方法,用这两个方法来确保联合主键的唯一性。

下面具体来看一个例子。
先看第一种实现方式。

第一种:使用映射文件XXX.hbm.xml来实现联合主键的映射

a、实体类 Student

package com.hibernate.model;
public class Student {
    private StudentPK pk;//主键
    public StudentPK getPk() {
        return pk;
    }
    public void setPk(StudentPK pk) {
        this.pk = pk;
    }
    private int age;

    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }   
}

上面的private StudentPK pk;就是我们的联合主键,联合主键我们封装在了一个类StudentPK中,也可以之间将联合主键的属性不进行封装,直接写在实体类Student。

b、联合主键类 StudentPK

这个就是对联合主键id和name进行了封装,即使得StudentPK为联合主键类,这个类要实现Serializable接口,并需要重写equals和hashCode方法。来实现组合的标识符的相等判断

package com.hibernate.model;
    import java.io.Serializable;
public class StudentPK implements Serializable{
   
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public boolean equals(Object o){
        if (o instanceof StudentPK) {
            StudentPK pk = (StudentPK) o;
            if(this.id==pk.getId()&&this.name.equals(pk.getName())){
                return true;
            }

        }
        return false ;
    }
    @Override
    public int hashCode(){
        return this.name.hashCode();
    }
}

c、联合主键的映射文件 Student.hbm.xml

在前面的例子中我们见到过一个属性作为主键的映射文件如何来写,下面就是几个属性作为主键的映射文件应该如何来写。

在官方文档上有这样一句话:

定义可以访问旧式的多主键数据。 我们强烈不建议使用这种方式。

文档上面给出的composite-id的一般格式如下:

<composite-id
        name="propertyName"
        class="ClassName"
        mapped="true|false"
        access="field|property|ClassName"
        node="element-name|."
        >

        <key-property name="propertyName" type="typename" column="column_name"/>
        <key-many-to-one name="propertyName class="ClassName" column="column_name"/>
        ......
    </composite-id>

在我们的例子中完整的映射文件内容如下:

<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.hibernate.model">
    <class name="Student" table="Student"> <!-- 如果我们不写table,则默认表名与类名一致 -->
        <!-- 联合主键 -->
        <composite-id name="pk" class="com.hibernate.model.StudentPK"></
Files contained in javax.persistence.jar: META-INF/MANIFEST.MF javax.persistence.Access.class javax.persistence.AccessType.class javax.persistence.AssociationOverride.class javax.persistence.AssociationOverrides.class javax.persistence.AttributeOverride.class javax.persistence.AttributeOverrides.class javax.persistence.Basic.class javax.persistence.Cache.class javax.persistence.Cacheable.class javax.persistence.CacheRetrieveMode.class javax.persistence.CacheStoreMode.class javax.persistence.CascadeType.class javax.persistence.CollectionTable.class javax.persistence.Column.class javax.persistence.ColumnResult.class javax.persistence.criteria.AbstractQuery.class javax.persistence.criteria.CollectionJoin.class javax.persistence.criteria.CompoundSelection.class javax.persistence.criteria.CriteriaBuilder.class javax.persistence.criteria.CriteriaQuery.class javax.persistence.criteria.Expression.class javax.persistence.criteria.Fetch.class javax.persistence.criteria.FetchParent.class javax.persistence.criteria.From.class javax.persistence.criteria.Join.class javax.persistence.criteria.JoinType.class javax.persistence.criteria.ListJoin.class javax.persistence.criteria.MapJoin.class javax.persistence.criteria.Order.class javax.persistence.criteria.ParameterExpression.class javax.persistence.criteria.Path.class javax.persistence.criteria.PluralJoin.class javax.persistence.criteria.Predicate.class javax.persistence.criteria.Root.class javax.persistence.criteria.Selection.class javax.persistence.criteria.SetJoin.class javax.persistence.criteria.Subquery.class javax.persistence.DiscriminatorColumn.class javax.persistence.DiscriminatorType.class javax.persistence.DiscriminatorValue.class javax.persistence.ElementCollection.class javax.persistence.Embeddable.class javax.persistence.Embedded.class javax.persistence.EmbeddedId.class javax.persistence.Entity.class javax.persistence.EntityExistsException.class javax.persistence.EntityListeners.class javax.persistence.EntityManager.c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值