hibernate 联合主键设置

1.使用 xml 设置的方式

1.1.Student外键

package com.dada;

import java.io.Serializable;

public class StudentPk implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	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 obj) {
		if (obj instanceof StudentPk) {
			StudentPk pk = (StudentPk) obj;
			if (pk.getId() == this.id && pk.getName().equals(this.getName())) {
				return true;
			}
		}
		return false;
	}
	
	@Override
	public int hashCode() {
		return this.name.hashCode();
	}
	
}
1.2.Student实体

package com.dada;

public class Student {
	private StudentPk pk;
	private int age;
	private String sex;
	private boolean good;
	
	public StudentPk getPk() {
		return pk;
	}
	public void setPk(StudentPk pk) {
		this.pk = pk;
	}
	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 boolean isGood() {
		return good;
	}
	public void setGood(boolean good) {
		this.good = good;
	}
}
1.3.配置文件 Student.hbm.xml

<?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>
	<class name="com.dada.Student" table="_student1">
	<composite-id name="pk" class="com.dada.StudentPk">
		<key-property name="id"/>
		<key-property name="name"/>
	</composite-id>
		<property name="age"/>
		<property name="sex"/>
		<property name="good" type="yes_no"/>
	</class>
</hibernate-mapping>


1.4.测试:

public void saveStudent() {
		Session session = sf.openSession();
		session.beginTransaction();
		StudentPk pk = new StudentPk();
		pk.setId(2);
		pk.setName("test1");
		Student student = new Student();
		student.setPk(pk);
		student.setAge(23);
		student.setGood(false);
		student.setSex(Gender.FEMAL.toString());
		session.save(student);
		session.getTransaction().commit();
		session.close();
	}

2.使用注解配置

2.1.Teacher 外键

package com.dada;

import java.io.Serializable;

public class TeacherPK 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 int hashCode() {
		return this.name.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof TeacherPK) {
			TeacherPK pk = (TeacherPK) obj;
			if (pk.getId() == this.id && pk.getName().equals(this.name)) {
				return true;
			}
		}
		return false;
	}
}

2.2.Teacher 实体

package com.dada;

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name="_teacher1")
@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;
	}
	private String title;
	private String yourWifeName;
	private Date birthday;
	private boolean good;
	private Gender gender;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	@Transient
	public String getYourWifeName() {
		return yourWifeName;
	}
	public void setYourWifeName(String yourWifeName) {
		this.yourWifeName = yourWifeName;
	}
	@Temporal(TemporalType.TIME)
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public boolean isGood() {
		return good;
	}
	public void setGood(boolean good) {
		this.good = good;
	}
	@Enumerated(EnumType.STRING)
	public Gender getGender() {
		return gender;
	}
	public void setGender(Gender gender) {
		this.gender = gender;
	}
	
}
2.3.测试 Teacher

public void testSaveTeacher() {
		Session session = sf.openSession();
		session.beginTransaction();
		Teacher t = new Teacher();
		t.setId(1);
		t.setName("teacher1");
		t.setGender(Gender.MALE);
		t.setGood(true);
		t.setBirthday(new Date());
		t.setTitle("语文");
		t.setYourWifeName("dandan");
		
		session.saveOrUpdate(t);
		session.getTransaction().commit();
		session.close();
	}

总结:使用配置文件的设置方式,需要在实体类中把主键作为一个属性引入进来,而使用注解的方式只需要把主键类定义放到实体类中就行了,不过联合主键的属性还是需要定义,而且需要在每一个联合主键的字段上加上 @Id 注解。

你可能感兴趣的:(hibernate 联合主键设置)