hibernate配置文件中的properties属性

假如现在有班级Class类与学生student类,但是在映射时,学生表关联的外键不是班级表的主键id,而是年级(grade)与班号(numb)这两个联合作外键,这样就要用到hibernate里的properties属性了。

 

Class.java类如下:

 

package cn.ehoo.hibernate.domain;
/**
 *@author whp
 *@Email [email protected]
 *@Aug 15, 2011
 *
 */
public class Class {
	private Long id;
	private String grade;
	private String number;
	public String getGrade() {
		return grade;
	}
	public void setGrade(String grade) {
		this.grade = grade;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	
	
}

 

Class.hbm.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 package="cn.ehoo.hibernate.domain">
	<class name="Class" table="class">
		<id name="id" type="long">
			<generator class="native" />
		</id>
		<properties name="name" unique="true" update="false">
			<property name="grade" />
			<property name="number" />
		</properties>

	</class>
</hibernate-mapping>

 

 

 

Student.java类如下:

 

package cn.ehoo.hibernate.domain;
/**
 *@author whp
 *@Email [email protected]
 *@Aug 15, 2011
 *
 */
public class Student {
	private Long id;
	private String name;
	private Class cla;
	public Class getCla() {
		return cla;
	}
	public void setCla(Class cla) {
		this.cla = cla;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

 

Student.hbm.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 package="cn.ehoo.hibernate.domain">
	<class name="Student" table="student">
		<id name="id" type="long">
			<generator class="native" />
		</id>
		<many-to-one name="cla" class="Class" property-ref="name">
			<column name="grade"></column>
			<column name="number"></column>
		</many-to-one>
	</class>
</hibernate-mapping>

 

 

你可能感兴趣的:(java,Hibernate,properties)