Hibernate联合主键生成策略


第一种XML

组件类:StudentPK.java

package com.test.hiberenate.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;
	}

	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;
	}

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

}

 实体类:Student.java

package com.test.hiberenate.model;

public class Student {

	private StudentPK pk;
	private int age;
	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 boolean isGood() {
		return good;
	}

	public void setGood(boolean good) {
		this.good = good;
	}

}

 实体类映射:Student.hbm.xml





	
		
			
			
		
		
		
	


 

第二种annotation

组件类:TeacherPK.java

package com.test.hiberenate.model;

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;
	}

	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;
	}

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

}

 实体类:Teacher.java

package com.test.hiberenate.model;

import java.util.Date;

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

/**
 * 定义组合主键的三种语法: 
 * 1.将组件类注解为@Embeddable,并将实体类中组件的属性注解为@Id
 * 2.将实体类中组件的属性注解为@EmbeddedId
 * 3.将实体类注解为@IdClass,并将该实体类中所有属于主键的属性都注解为@Id
 */
@Entity
@IdClass(TeacherPK.class)
public class Teacher {
	private int id;
	private String name;
	private String title;
	private String description;
	private Date birthday;

	@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;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	@Transient
	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Temporal(TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

}

 

1.组件类序列化(为什么)

对于一个实体类来说,它在数据库表里面可能存在着多条记录,如果把这多条记录都放到内存里面的话就是多个对象,每个对象里面都有一个主键对象,情况一,如果系统要做成集群,这台服务器宕机了或有问题了,可以把这个服务器里面的对象传给另外一台服务器,这个时候就需要序列化从而进行对象传递。情况二,如果服务器的内存满了,这个时候可以使用虚拟内存(就是把硬盘上的一部分空间作为内存使用),这种情况下,就要把内存里面的对象暂时放到硬盘中去,这个时候就要序列化。

2.组件类重写hashCodeequal方法(为什么)

为了保证对象唯一性的。多个对象放在内存中,他们之间用什么区分?重写组件类的hashCode()equal()方法来区分,数据库中是用那个联合主键来区分的。

你可能感兴趣的:(hibernate)