欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hibernate 注解方式实现联合主键 博客分类: Hibernate hibernate注解联合主键 

程序员文章站 2024-03-19 10:56:22
...

联合主键的实现方式有三种

方式1.将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,再将该类注解为@Embeddable,最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注解为@Id

 

@Embeddable
public class DogPrimarykey implements Serializable {

	private static final long serialVersionUID = 2555027553374060423L;

	private String id;
	@Column(name="DOG_NAME")
	private String name;

	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;
		DogPrimarykey other = (DogPrimarykey) 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;
	}

}

 

@Entity(name = "DOG")
public class Dog {
	private static final long serialVersionUID = 7219216851705947642L;
	@Id
	private DogPrimarykey dogPrimarykey;

	@Column(name = "DOG_AGE", length = 2)
	private int age;

	public DogPrimarykey getDogPrimarykey() {
		return dogPrimarykey;
	}

	public void setDogPrimarykey(DogPrimarykey dogPrimarykey) {
		this.dogPrimarykey = dogPrimarykey;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age > 100 || age < 1) {
			this.age = 1;
		} else {
			this.age = age;
		}
	}

	public void say() {
		// TODO Auto-generated method stub
		System.out.println("汪  汪  汪");
	}

	@Override
	public String toString() {
		return "Dog [dogPrimarykey=" + dogPrimarykey + ", age=" + age + "]";
	}

}

 方式2:将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并重写equals和hascode,最后在主类中(该类不包含联合主键类中的字段)保存该联合主键类的一个引用,并生成set和get方法,并将该引用注解为@EmbeddedId

 

public class DogPrimarykey implements Serializable {

	private static final long serialVersionUID = 2555027553374060423L;

	private String id;
	@Column(name="DOG_NAME")
	private String name;

	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;
		DogPrimarykey other = (DogPrimarykey) 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;
	}
}

 

@Entity(name = "DOG")
public class Dog {
	private static final long serialVersionUID = 7219216851705947642L;
	@EmbeddedId
	private DogPrimarykey dogPrimarykey;

	@Column(name = "DOG_AGE", length = 2)
	private int age;

	public DogPrimarykey getDogPrimarykey() {
		return dogPrimarykey;
	}

	public void setDogPrimarykey(DogPrimarykey dogPrimarykey) {
		this.dogPrimarykey = dogPrimarykey;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age > 100 || age < 1) {
			this.age = 1;
		} else {
			this.age = age;
		}
	}

	public void say() {
		// TODO Auto-generated method stub
		System.out.println("汪  汪  汪");
	}

	@Override
	public String toString() {
		return "Dog [dogPrimarykey=" + dogPrimarykey + ", age=" + age + "]";
	}

}

 方式3:将联合主键的字段单独放在一个类中,该类需要实现java.io.Serializable接口并要重写equals和hashcode.最后在主类中(该类包含联合主键类中的字段)将联合主键字段都注解为@Id,并在该类上方加上这样的注解:@IdClass(联合主键类.class)

public class DogPrimarykey implements Serializable {

	private static final long serialVersionUID = 2555027553374060423L;

	private String id;
	@Column(name="DOG_NAME")
	private String name;

	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;
		DogPrimarykey other = (DogPrimarykey) 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;
	}
}

 

@Entity(name = "DOG")
@IdClass(DogPrimarykey.class)
public class Dog {
	private static final long serialVersionUID = 7219216851705947642L;

	@Id
	private String id;
	@Id
	private String name;
	@Column(name = "DOG_AGE", length = 2)
	private int age;

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

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		if (age > 100 || age < 1) {
			this.age = 1;
		} else {
			this.age = age;
		}
	}

	public void say() {
		// TODO Auto-generated method stub
		System.out.println("汪  汪  汪");
	}

	@Override
	public String toString() {
		return "Dog [id=" + id + ", name=" + name + ", age=" + age + "]";
	}
}