ID生成策略:联合主键xml

前端之家收集整理的这篇文章主要介绍了ID生成策略:联合主键xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在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>

猜你在找的XML相关文章