Hibernate学习之--------联合主键的注解使用

 联合主键的注解使用:
 1.使用@Id来申明主键,同时将该主键的类用@Embeddable设置
      如:

//联合主键对象类
		/*
		 * 注意在hashcode和equals方法的作用,在系统中所有相同的hashcode值得对象都存到一个数组中,
		 * 这个数组的么一个元素都是一个队列,然后当我们需哟啊找到每个对象时,就查找该对象的hashcode进而找出该对象的
		 * 在数组中的位置,然后再取出队列遍历用每个对象的equals方法判断该对象是否与查找的对象相等,知直到找到相等的
		 * 对象,否则将会出现空指针异常
		 */
		@Embeddable
		public class TeacherPK implements Serializable {
		
			private int id;
			
			private String name;
		
			public String getName() {
				return name;
			}
		
			public void setName(String name) {
				this.name = name;
			}
		
			public int getId() {
				return id;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			@Override
			public boolean equals(Object obj) {
				if(obj instanceof TeacherPK){
					TeacherPK pk = (TeacherPK) obj;
					if(this.id == pk.getId()&&this.name.equals(pk.getName())){
						return true;
					}
				}
				return false;
			}
			
			@Override
			public int hashCode() {
				return this.name.hashCode();
			}
		}
		
		public class Teacher {
			
			private TeacherPK pk;
			
			@Id
			public TeacherPK getPk() {
				return pk;
			}
		
			public void setPk(TeacherPK pk) {
				this.pk = pk;
			}
		
			private String title;
		
			public void setTitle(String title) {
				this.title = title;
			}
			
			public String getTitle() {
				return title;
			}
		}

 2.使用@Id将多个属性申明为主键,另外在该类的上使用@IdClass(value="你需要符合的类")
  如:

 

@IdClass(value="TeacherPK.class")
		public class Teacher{
			private int id;
			private String name;
			
			private String title;
			
			@Id
			public int getId() {
				return id;
			}
			@Id
			public String getName() {
				return name;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			public void setName(String name) {
				this.name = name;
			}
			
			public void setTitle(String title) {
				this.title = title;
			}
			
			public String getTitle() {
				return title;
			}
		}
		
		public class TeacherPK implements Serializable {

			private int id;
			
			private String name;
		
			public String getName() {
				return name;
			}
		
			public void setName(String name) {
				this.name = name;
			}
		
			public int getId() {
				return id;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			@Override
			public boolean equals(Object obj) {
				if(obj instanceof TeacherPK){
					TeacherPK pk = (TeacherPK) obj;
					if(this.id == pk.getId()&&this.name.equals(pk.getName())){
						return true;
					}
				}
				return false;
			}
			
			@Override
			public int hashCode() {
				return this.name.hashCode();
			}
		}

 3.将主键属性设置@EmbeddedId申明即可,主键类可不申明任何注解
  如:

 

 

//主键类
		public class TeacherPK implements Serializable {
		
			private int id;
			
			private String name;
		
			public String getName() {
				return name;
			}
		
			public void setName(String name) {
				this.name = name;
			}
		
			public int getId() {
				return id;
			}
		
			public void setId(int id) {
				this.id = id;
			}
		
			@Override
			public boolean equals(Object obj) {
				if(obj instanceof TeacherPK){
					TeacherPK pk = (TeacherPK) obj;
					if(this.id == pk.getId()&&this.name.equals(pk.getName())){
						return true;
					}
				}
				return false;
			}
			
			@Override
			public int hashCode() {
				return this.name.hashCode();
			}
		}
		
		public class Teacher {
			
			private TeacherPK pk;
			
			@EmbeddedId
			public TeacherPK getPk() {
				return pk;
			}
		
			public void setPk(TeacherPK pk) {
				this.pk = pk;
			}
		
			private String title;
		
			public void setTitle(String title) {
				this.title = title;
			}
			
			public String getTitle() {
				return title;
			}
		}

 这三种方式各有各的好处,个人认为在开发中第三种比较常用。

注意:联合主键不会生成新的表。

你可能感兴趣的:(注解,Hibernate)