XML:
        <composite-id name="pk" class="StudentPK">
        
<key-property name="id"></key-property>
        
<key-property name="name"></key-property>
        
</composite-id>
StudentPK.java
package com.bebig.hibernate.model;

import java.io.Serializable;

public class StudentPK implements Serializable {
    
private int id;
    
private String name;

    
public void setName(String name) {
        
this.name = name;
    }


    
public String getName() {
        
return name;
    }


    
public void setId(int id) {
        
this.id = id;
    }


    
public int getId() {
        
return id;
    }


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

}

Student.java
package com.bebig.hibernate.model;

/**
 * 
@author fenglin
 * @filename Student.java
 
*/

public class Student {
    
private StudentPK pk;
    
private int age;

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


}

测试用例
    @Test
    
public void TestStudentSave() {
        StudentPK pk 
= new StudentPK();
        Student s 
= new Student();
        pk.setId(
1);
        pk.setName(
"haha");
        s.setPk(pk);
        s.setAge(
20);

        Session session 
= sessionFactory.openSession();
        session.beginTransaction();
        session.save(s);
        session.getTransaction().commit();

        session.close();

    }