hibernate复合主键映射(基础应用)

一般来说,应用中的主键都是以表中的一列作为主键(也有无主键的表),独立主键的使用在hibernate中已经比较常见了,复合主键使用的场合一般用于复杂记录的时候。

例:在一个企业中,可能有两个或者更多的人同名,此时名称已经区分不出他们了,但是可以区分的是他们的身份证编号也就是ID号

在hibernate中也可以使用这种方式加以区分,就成为复合主键的映射

将需要的主键独立成一个类

示例如下

package vo;

/**
 * 实体类
 * @author Wang_yx
 *
 */
public class Person {

	private PersonPK pk = new PersonPK();
	private String personEmail;
	private String personAddress;
	public PersonPK getPk() {
		return pk;
	}
	public void setPk(PersonPK pk) {
		this.pk = pk;
	}
	public String getPersonEmail() {
		return personEmail;
	}
	public void setPersonEmail(String personEmail) {
		this.personEmail = personEmail;
	}
	public String getPersonAddress() {
		return personAddress;
	}
	public void setPersonAddress(String personAddress) {
		this.personAddress = personAddress;
	}
   }
  
package vo;

/**
 * 复合主键类必须实现序列化接口
 * @author Wang_yx
 *
 */
public class PersonPK implements java.io.Serializable{

	private Integer id;
	private String personName;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getPersonName() {
		return personName;
	}
	public void setPersonName(String personName) {
		this.personName = personName;
	}
	
}

hibernate.cfg.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="vo.Person">
	 <!-- 复合主键的映射 此处的name属性必须和实体中的对象名一致-->
     <composite-id name="pk">
       <key-property name="id" column="id"></key-property>
       <key-property name="personName" column="personname"></key-property>
     </composite-id>
     
     <!-- 普通属性的映射 -->
	 <property name="personEmail" column="personemail"></property>
	 <property name="personAddress" column="personaddress"></property>
	</class>
	
</hibernate-mapping>

测试方法

package test;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import vo.Person;
import vo.PersonPK;

public class HibernateTest {

    private static Configuration config = new Configuration().configure();
	private static  SessionFactory factory = null;
    static{
    	factory = config.buildSessionFactory();
    }
    public static void loadPerson(){
    	 Session session = factory.openSession();
    	 Transaction tr = session.getTransaction();
    	 PersonPK pk = new PersonPK();
    	 pk.setId(new Integer(1));
    	 pk.setPersonName("张三");
    	 try{
    		 tr.begin();
    		 Person person = (Person) session.get(Person.class,pk);
    		 if(person!=null){
    			 System.out.print(person.getPersonAddress() + " ");
    			 System.out.println(person.getPersonEmail());
    		 }
    		 
    		 tr.commit();
    	 }catch(HibernateException e){
    		 e.printStackTrace();
    	 }finally{
    		 session.close();
    	 }
     }
	
	 /**
	 * 复合主键测试
	 */
	public static void main(String[] args) {
		loadPerson();

	}

}

相关推荐