联合主键Annotation配置方法有三种:
1.将组件类注解为@EmbeddedId,并将组建的属性注解为@Id
2.将组件的属性注解为@EmbeddeId
3.将类注解为@IdClass,并将该实体中所有的主键的属性都注解为@Id
第一种:
1.定义注解类并其直接为@Embeddable
package com.zchen.hibernate.sxt.domain; import java.io.Serializable; import javax.persistence.Embeddable; @Embeddable public class StudentPK implements Serializable{ private static final long serialVersionUID = -7880746333829652369L; 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(); }*/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; 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; StudentPK other = (StudentPK) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
2.定义实体类,将注解类放入其中并注解为@Id
package com.zchen.hibernate.sxt.domain; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_STUDENTS") public class Students { private StudentPK pk; @Id 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } private int age; private String address; }
3.定义Hibernate.cfg.xml配置文件
<?xml version="1.0"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///db_czbk_hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">create</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property> <property name="show_sql">true</property> <mapping class="com.zchen.hibernate.sxt.domain.Students"/> </session-factory> </hibernate-configuration>
4.测试:
package com.zchen.hibernate.sxt.test; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.zchen.hibernate.sxt.domain.StudentPK; import com.zchen.hibernate.sxt.domain.Students; public class StudentsTest { private static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ try { sf = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); } } @Test public void addStduentTest(){ StudentPK pk = new StudentPK(); pk.setId(2); pk.setName("chen"); Students s = new Students(); s.setAge(20); s.setAddress("湖南"); s.setPk(pk); Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); } @AfterClass public static void afterClass(){ if(sf != null){ try { sf.close(); } catch (HibernateException e) { e.printStackTrace(); } } } }
第二种:
1.定义主键类
package com.zchen.hibernate.sxt.domain; import java.io.Serializable; import javax.persistence.Embeddable; //1.@Embeddable public class StudentPK implements Serializable{ private static final long serialVersionUID = -7880746333829652369L; 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(); }*/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; 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; StudentPK other = (StudentPK) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
2.定义实体类
package com.zchen.hibernate.sxt.domain; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="T_STUDENTS") public class Students { private StudentPK pk; //@Id @EmbeddedId 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } private int age; private String address; }
第三种:
1.定义主键类:
package com.zchen.hibernate.sxt.domain; import java.io.Serializable; import javax.persistence.Embeddable; public class StudentPK implements Serializable{ private static final long serialVersionUID = -7880746333829652369L; 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(); }*/ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; 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; StudentPK other = (StudentPK) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }
2.定义实体类:
package com.zchen.hibernate.sxt.domain; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @Entity @Table(name="T_STUDENTS") @IdClass(value=StudentPK.class) public class Students { /*private StudentPK pk; //@Id @EmbeddedId public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this.pk = pk; }*/ 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; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } private int age; private String address; }
3.测试用例:
package com.zchen.hibernate.sxt.test; import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import com.zchen.hibernate.sxt.domain.StudentPK; import com.zchen.hibernate.sxt.domain.Students; public class StudentsTest { private static SessionFactory sf = null; @BeforeClass public static void beforeClass(){ try { sf = new AnnotationConfiguration().configure().buildSessionFactory(); } catch (HibernateException e) { e.printStackTrace(); } } @Test public void addStduentTest(){ /*StudentPK pk = new StudentPK(); pk.setId(3); pk.setName("aaa");*/ Students s = new Students(); s.setId(4); s.setName("pig"); s.setAge(20); s.setAddress("湖南"); //s.setPk(pk); Session session = sf.getCurrentSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); } @AfterClass public static void afterClass(){ if(sf != null){ try { sf.close(); } catch (HibernateException e) { e.printStackTrace(); } } } }