Hibernate学习笔记_联合主键

复合主键(联合主键):多个字段构成唯一性。

一,xml方式

 

1. 将联合主键的属性提取出来,重新编写一个StudentPK类(原Student类中的id,name要删除 并新加入属性“StudentPK”)

//StudentPK .java
package
com.bjsxt.hibernate; public class StudentPK implements java.io.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(); } }
//Student .java
package com.bjsxt.hibernate;

public class Student {
    
    private StudentPK pk;
    
    private int age;
    private String sex;
    private boolean good;
    public boolean isGood() {
        return good;
    }
    public void setGood(boolean good) {
        this.good = good;
    }
    /*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;
    }*/
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public StudentPK getPk() {
        return pk;
    }
    public void setPk(StudentPK pk) {
        this.pk = pk;
    }
    
    
}

注意:

         i.   新建StudentPK 类必须实现 java.io.Serializable 序列化接口(把类对象序列化)

         ii.   新StudentPK 类要重写equals和hashCode方法(保证唯一性)

2.  联合主键生成策略XML配置方法

<hibernate-mapping>
    <class name="com.bjsxt.hibernate.Student">

        <composite-id name="pk" class="com.bjsxt.hibernate.StudentPK">
            <key-property name="id"></key-property>
            <key-property name="name"></key-property>
        </composite-id>
        
        <property name="age" />
        <property name="sex" />
        <property name="good" type="yes_no"></property>
    </class>
    
</hibernate-mapping>

 二,Annotation

   1.前三步与Xml方式前三步一样 都要建立新的类 都要实现Serializable接口 重写equals和hashCode方法.

package com.bjsxt.hibernate;

import javax.persistence.Embeddable;

@Embeddable
public class TeacherPK implements java.io.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 TeacherPK) {
            TeacherPK pk = (TeacherPK)o;
            if(this.id == pk.getId() && this.name.equals(pk.getName())) {
                return true;
            }
        }
        return false;
    }
    
    @Override
    public int hashCode() {
        return this.name.hashCode();
    }
}

 

 

 

package com.bjsxt.hibernate;

import java.util.Date;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.IdClass;
import javax.persistence.SequenceGenerator;

import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@javax.persistence.TableGenerator(
        name="Teacher_GEN",
        table="GENERATOR_TABLE",
        pkColumnName = "pk_key",
        valueColumnName = "pk_value",
        pkColumnValue="Teacher",
        allocationSize=1
    )

@SequenceGenerator(name="teacherSEQ", sequenceName="teacherSEQ_DB")

public class Teacher {

    private TeacherPK pk;
    
    private String title; 
    private String yourWifeName;
    private Date birthDate;
    private boolean good;
    private Gender gender;
    
    
    public Gender getGender() {
        return gender;
    }
    public void setGender(Gender gender) {
        this.gender = gender;
    }
    public boolean isGood() {
        return good;
    }
    public void setGood(boolean good) {
        this.good = good;
    }
    @Transient
    public String getYourWifeName() {
        return yourWifeName;
    }
    public void setYourWifeName(String yourWifeName) {
        this.yourWifeName = yourWifeName;
    }
    
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    @Temporal(TemporalType.TIME)
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    
    @Id
    public TeacherPK getPk() {
        return pk;
    }
    public void setPk(TeacherPK pk) {
        this.pk = pk;
    }
}

 

 

 方法一:

 在新类前写@Embeddable,在原类的新属性“TercherPK”的get方法前写@ld,

//TeacherPK .java
@Embeddable
public class TeacherPK implements java.io.Serializable{ private int id; private String name; 。。。。。。。 }

 

 

//Teacher .java
@Entity
public class Teacher {

    private TeacherPK pk;
.......................
    
    @Id
    public TeacherPK getPk() {
        return pk;
    }
    public void setPk(TeacherPK pk) {
        this.pk = pk;
    }
}

 

 

方法2:

新的TeacherPK 类无需加注解,只需在原Teacher类新属性“TercherPK”的get方法前写@EmbeddedlD即可

@EmbeddedId
    public TeacherPK getPk() {
        return pk;
    }
    public void setPk(TeacherPK pk) {
        this.pk = pk;
    }

 

 方法3:

TeacherPK 类无需加注解,原Teacher类的id,name属性保留不变,也无需新增“TercherPK”属性。 只在id,name的get方法前都加@Id,并在原Teacher类前加“@IdClass(TeacherPK.class)”

@IdClass(TeacherPK.class)
public class Teacher {

    private int id;
    
    private String name;
    。。。。。

    @Id
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    @Id
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
。。。。。。
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。