在Student.java中,假设id和name作为它的主键
1、在com.bjsxt.hibernate下单独设计一个类,做为主键类【StudentPK】
package com.bjsxt.hibernate; public class StudentPK implements java.io.Serializable { 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; } }
2、在Student.java中,建
private StudentPK pk;
public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this.pk = pk; }
3、在HibernateIDTest.java中:
@Test public void testStudentSave() { StudentPK pk=new StudentPK(); pk.setId(1); pk.setName("zhangsan");
4、在Student.hbm.xml中:
<pre class="html" name="code"><composite-id name="pk" class="com.bjsxt.hibernate.StudentPK"> <key-property name="id"></key-property> <key-property name="name"></key-property> </composite-id>原文链接:https://www.f2er.com/xml/298277.html