组件属性包含关联实体的一个例子

set get 方法省略:

1:Person类

package zhorj.hibernate.component.correlation;

public class Person {
	private Integer id; // 标识属性
	private String name; //
	private Address addr;
	}


2:Address 类

package zhorj.hibernate.component.correlation;
import java.util.HashSet;
import java.util.Set;
public class Address {
	private int addrId;
	private Person peson;
	private String addressDetail;
	Set<School> schools = new HashSet<School>();
	public Address() {
	}
	public Address(String addressDetail) {
		this.addressDetail = addressDetail;
	}	
}

 

3:School类

public class School {
 private int id;
 private String name;}


4:School.hbm.xml 的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="zhorj.hibernate.component.correlation">
	<class name="School" table="school_inf">
		<id name="id">
			<generator class="identity"></generator>
		</id>
		<property name="name" column="school_name" type="string" length="20"></property>
	</class>
</hibernate-mapping>


5:Person.hbm.xml 的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="zhorj.hibernate.component.correlation">
	<class name="Person" table="person_inf">
		<id name="id" column="preson_id">
			<generator class="identity"></generator>
		</id>
		<property name="name" column="p_name" type="string"></property>
		<component name="addr" class="Address">
			<parent name="peson"/>
			<property name="addressDetail"/>
			<set name="schools" >
				<!--指定外键关联列  -->
				<key column="addr_id"></key>
				<one-to-many class="School"/>
			</set>
		</component>
	</class>
</hibernate-mapping>


执行后数据库表

组件属性包含关联实体的一个例子_第1张图片
 

你可能感兴趣的:(组件属性包含关联实体的一个例子)