Hibernate关联关系映射配置 2
一、 一对一单向外键关联: 2
1.1目录结构 2
1.2 Annotation方式 2
1.3 XML方式 4
1.4 Hibernate配置文件 7
二、一对一双向外键关联 7
2.1 Annotation方式 7
2.2 XML方式 9
三、一对一单向主键关联(不重要) 12
3.1 Annotation方式 12
3.2 XML方式 14
四、一对一双向主键关联(不重要) 16
4.1 Annotation方式 16
3.2 XML方式 19
五、组件映射 21
5.1 Annotation方式 21
5.2 XML方式 23
六、多对一单向关联 25
6.1 Annotation方式 25
6.2 XML方式 27
七、一对多单向关联 29
7.1 Annotation方式 29
7.2 XML方式 31
八、一对多、多对一双向关联 34
8.1 Annotation方式 34
8.2 XML方式 36
九、多对多单向关联 39
8.1 Annotation方式 39
8.2 XML方式 41
十、多对多双向关联 44
8.1 Annotation方式 44
8.2 XML方式 46
图1-1 目录结构图
1.2.1 类图
图1-2 类关联关系图
1.2.2数据库表结构
图1-3数据库表结构图
1.2.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HUSBAND_")
@SequenceGenerator(name="husband_seq_gene", sequenceName="HUSBAND_SEQ", allocationSize=1)
public class Husband {
private BigDecimal id;
private String name;
private Wife wife;
@Id
@Column(precision=4, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="husband_seq_gene")
public BigDecimal getId() {
return id;
}
@Column(length=30)
public String getName() {
return name;
}
@OneToOne
//@JoinColumn(name="wife_id")//不写也没问题,自动生成的字段也叫wife_id
public Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity(name="H_WIFE_")
@SequenceGenerator(name="wife_seq_gene", sequenceName="WIFE_SEQ")
public class Wife {
private BigDecimal id;
private String name;
@Id
@Column(precision=4)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="wife_seq_gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.3.1 类图
图1-4 类关联关系图
1.3.2 数据库表结构
图1-5数据库表结构图
1.3.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Husband {
private BigDecimal id;
private String name;
private XML_Wife wife;
public BigDecimal getId() {
return id;
}
public String getName() {
return name;
}
public XML_Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(XML_Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Wife {
private BigDecimal id;
private String name;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
1.3.4 对象关系映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Husband" table="H_HUSBAND_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30"></property>
<many-to-one name="wife" column="wife_id" unique="true" not-null="true" />
<!--many-to-one是站在当前类XML_Husband的角度来看,XML_Husband与wife是多对一-->
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Wife" table="H_Wife_XML" dynamic-update="true">
<id name="id">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30" />
</class>
</hibernate-mapping>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:silence</property>
<property name="connection.username">cnuser</property>
<property name="connection.password">cn830306</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<!--<property name="hbm2ddl.auto">update</property>-->
<mapping class="com.rongqq.hibernate3.annotation.entity.Husband" />
<mapping class="com.rongqq.hibernate3.annotation.entity.Wife" />
<mapping resource="com/rongqq/hibernate3/xml/entity/XML_Husband.hbm.xml" />
<mapping resource="com/rongqq/hibernate3/xml/entity/XML_Wife.hbm.xml" />
</session-factory>
</hibernate-configuration>
2.1.1 类图
图2-1类关联关系图
2.1.2 数据库结构图
图2-2数据库表结构图
2.1.3 实体类
@Entity(name="H_HUSBAND_")
@SequenceGenerator(name="husband_seq_gene", sequenceName="HUSBAND_SEQ", allocationSize=1)
public class Husband {
private BigDecimal id;
private String name;
private Wife wife;
@Id
@Column(precision=4, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="husband_seq_gene")
public BigDecimal getId() {
return id;
}
@Column(length=30)
public String getName() {
return name;
}
@OneToOne
//@JoinColumn(name="wife_id")//不写也没问题,自动生成的字段也叫wife_id
public Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
@Entity(name="H_WIFE_")
@SequenceGenerator(name="wife_seq_gene", sequenceName="WIFE_SEQ")
public class Wife {
private BigDecimal id;
private String name;
private Husband husband;
@Id
@Column(precision=4)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="wife_seq_gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne(mappedBy="wife")
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
2.2.1 类图
图2-3 类关联关系图
2.2.2 数据库结构图
2.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Husband {
private BigDecimal id;
private String name;
private XML_Wife wife;
public BigDecimal getId() {
return id;
}
public String getName() {
return name;
}
public XML_Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(XML_Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Wife {
private BigDecimal id;
private String name;
private XML_Husband husband;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public XML_Husband getHusband() {
return husband;
}
public void setHusband(XML_Husband husband) {
this.husband = husband;
}
}
2.2.4 对象关系映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Husband" table="H_HUSBAND_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30"></property>
<many-to-one name="wife" unique="true" not-null="true" >
<column name="wife_id" precision="4"></column>
</many-to-one>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Wife" table="H_Wife_XML" dynamic-update="true">
<id name="id">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30" />
<!-- 这里的wife是XML_Husband类中的属性名,表示这里的关联关系已经由Husband的对象
映射文件的wife属性上定义过了 -->
<one-to-one name="husband" property-ref="wife" />
</class>
</hibernate-mapping>
3.1.1 类图
图3-1 类关联关系图
3.1.2 数据库表结构
图3-2 数据库表结构图
3.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HUSBAND_")
@SequenceGenerator(name="husband_seq_gene", sequenceName="HUSBAND_SEQ", allocationSize=1)
public class Husband {
private BigDecimal id;
private String name;
private Wife wife;
@Id
@Column(precision=4, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="husband_seq_gene")
public BigDecimal getId() {
return id;
}
@Column(length=30)
public String getName() {
return name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity(name="H_WIFE_")
@SequenceGenerator(name="wife_seq_gene", sequenceName="WIFE_SEQ")
public class Wife {
private BigDecimal id;
private String name;
@Id
@Column(precision=4)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="wife_seq_gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
注:只生成了表,但是没有外键约束,具体实现还需要查找
3.2.1 类图
图3-3 类关联关系图
3.2.2 数据库表结构
图3-4 数据库表结构图
3.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Husband {
private BigDecimal id;
private String name;
private XML_Wife wife;
public BigDecimal getId() {
return id;
}
public String getName() {
return name;
}
public XML_Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(XML_Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import com.rongqq.hibernate3.annotation.entity.Husband;
public class XML_Wife {
private BigDecimal id;
private String name;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Husband" table="H_HUSBAND_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="id" precision="4" scale="0" />
<generator class="foreign">
<!-- 指明具体参考哪一个外键,因为一张表可能存在多个外键 -->
<param name="property">wife</param>
</generator>
</id>
<property name="name" length="30"></property>
<one-to-one name="wife" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Wife" table="H_Wife_XML" dynamic-update="true">
<id name="id">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30" />
</class>
</hibernate-mapping>
4.1.1 类图
图4-1 类关联关系图
4.1.2 数据库表结构
图4-2 数据库表结构图
4.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HUSBAND_")
@SequenceGenerator(name="husband_seq_gene", sequenceName="HUSBAND_SEQ", allocationSize=1)
public class Husband {
private BigDecimal id;
private String name;
private Wife wife;
@Id
@Column(precision=4, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="husband_seq_gene")
public BigDecimal getId() {
return id;
}
@Column(length=30)
public String getName() {
return name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.SequenceGenerator;
@Entity(name="H_WIFE_")
@SequenceGenerator(name="wife_seq_gene", sequenceName="WIFE_SEQ")
public class Wife {
private BigDecimal id;
private String name;
private Husband husband;
@Id
@Column(precision=4)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="wife_seq_gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
@Column(length=30)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToOne
@PrimaryKeyJoinColumn
public Husband getHusband() {
return husband;
}
public void setHusband(Husband husband) {
this.husband = husband;
}
}
注:同样不产生关联关系,具体实现待查
4.2.1 类图
图4-3 类关联关系图
4.2.2 数据库表结构
图4-4 数据库表结构图
4.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Husband {
private BigDecimal id;
private String name;
private XML_Wife wife;
public BigDecimal getId() {
return id;
}
public String getName() {
return name;
}
public XML_Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(XML_Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Wife {
private BigDecimal id;
private String name;
private XML_Husband husband;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public XML_Husband getHusband() {
return husband;
}
public void setHusband(XML_Husband husband) {
this.husband = husband;
}
}
4.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Husband" table="H_HUSBAND_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="id" precision="4" scale="0" />
<generator class="foreign">
<param name="property">wife</param>
</generator>
</id>
<property name="name" length="30"></property>
<one-to-one name="wife" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Wife" table="H_Wife_XML" dynamic-update="true">
<id name="id">
<column name="id" precision="4" scale="0" />
<generator class="sequence">
<param name="sequence">H_STUDENT_SEQ_XML</param>
</generator>
</id>
<property name="name" length="30" />
<one-to-one name="husband" property-ref="wife"></one-to-one>
</class>
</hibernate-mapping>
5.1.1 类图
图5-1 类关联关系图
5.1.2 数据库表结构
图5-2 数据库表结构图
5.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HUSBAND_")
@SequenceGenerator(name="husband_seq_gene", sequenceName="HUSBAND_SEQ", allocationSize=1)
public class Husband {
private BigDecimal id;
private String name;
private Wife wife;
@Id
@Column(precision=4, scale=0)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="husband_seq_gene")
public BigDecimal getId() {
return id;
}
@Column(length=30)
public String getName() {
return name;
}
@Embedded
public Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
public class Wife {
private String name;
private BigDecimal age;
@Column(name="wifeName")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Column(precision=3)
public BigDecimal getAge() {
return age;
}
public void setAge(BigDecimal age) {
this.age = age;
}
}
5.2.1 类图
图5-3 类关联关系图
5.2.2 数据库表结构
图5-4 数据库表结构图
5.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Husband {
private BigDecimal id;
private String name;
private XML_Wife wife;
public BigDecimal getId() {
return id;
}
public String getName() {
return name;
}
public XML_Wife getWife() {
return wife;
}
public void setId(BigDecimal id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setWife(XML_Wife wife) {
this.wife = wife;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Wife {
private String name;
private BigDecimal age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getAge() {
return age;
}
public void setAge(BigDecimal age) {
this.age = age;
}
}
5.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Husband" table="H_HUSBAND_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="id" precision="4" scale="0" />
<generator class="foreign">
<param name="property">wife</param>
</generator>
</id>
<property name="name" length="30"></property>
<component name="wife">
<property name="name" column="WIFENAME" />
<property name="age" column="AGE" precision="3" />
</component>
</class>
</hibernate-mapping>
6.1.1 类图(说明:一个人具备多套房子的所有权,这么多套房子属于这个人)
图6-1 类关联关系图
6.1.2 数据库表结构
图6-2 数据库表结构图
6.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HOUSE_")
@SequenceGenerator(name="HouseGene", sequenceName="H_HOUSE_SEQ", allocationSize=1)
public class House {
private BigDecimal id;
private String position;
private Person person;//House(多)-Person(一),一个人可以有多个房子,而这些房子属于这一个人
@Id
@Column(precision=20)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="HouseGene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@ManyToOne
//@JoinColumn(name=””)//可以指定外键名称
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity(name="H_PERSON_")
@SequenceGenerator(name="PersonGene", sequenceName="H_PERSON_SEQ", allocationSize=1)
public class Person {
private BigDecimal Id;
private String name;
@Id
@Column(precision=21)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PersonGene")
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6.2.1 类图(说明:一个人具备多套房子的所有权,这么多套房子属于这个人)
图6-3 类关联关系图
6.2.2 数据库表结构
图6-4 数据库表结构图
6.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_House {
private BigDecimal id;
private String position;
private XML_Person person;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public XML_Person getPerson() {
return person;
}
public void setPerson(XML_Person person) {
this.person = person;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Person {
private BigDecimal Id;
private String name;
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_House" table="H_HOUSE_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">H_HOUSE_XML_SEQ</param>
</generator>
</id>
<property name="position"></property>
<many-to-one name="person">
<column name="PERSON_ID" precision="21" scale="0" />
</many-to-one>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Person" table="H_PERSON_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="21" scale="0" />
<generator class="sequence">
<param name="sequence">H_XML_PERSON_SEQ</param>
</generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
注:(表)设计原则,在多的一方加外键
7.1.1 类图(说明:一个人具备多套房子的所有权,这么多套房子属于这个人)
图7-1 类关联关系图
7.1.2 数据库表结构
图7-2 数据库表结构图
7.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
@Entity(name="H_PERSON_")
@SequenceGenerator(name="PersonGene", sequenceName="H_PERSON_SEQ", allocationSize=1)
public class Person {
private BigDecimal Id;
private String name;
private Set<House> houses = new HashSet<House>();
@Id
@Column(precision=21)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PersonGene")
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany
@JoinColumn(name="HOUSE_ID")//不加会生成由一张中间表连接的共三张表
public Set<House> getHouses() {
return houses;
}
public void setHouses(Set<House> houses) {
this.houses = houses;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HOUSE_")
@SequenceGenerator(name="HouseGene", sequenceName="H_HOUSE_SEQ", allocationSize=1)
public class House {
private BigDecimal id;
private String position;
@Id
@Column(precision=20)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="HouseGene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
7.2.1 类图(说明:一个人具备多套房子的所有权,这么多套房子属于这个人)
图7-3 类关联关系图
7.2.2 数据库表结构
图7-4 数据库表结构图
7.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class XML_Person {
private BigDecimal Id;
private String name;
private Set<XML_House> houses = new HashSet<XML_House>();
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<XML_House> getHouses() {
return houses;
}
public void setHouses(Set<XML_House> houses) {
this.houses = houses;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_House {
private BigDecimal id;
private String position;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
}
7.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Person" table="H_PERSON_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="21" scale="0" />
<generator class="sequence">
<param name="sequence">H_XML_PERSON_SEQ</param>
</generator>
</id>
<property name="name"/>
<set name="houses">
<key><!-- 指定H_HOUSE_XML表中的外键字段名,也可以<key column="PERSON_ID" /> -->
<column name="PERSON_ID" precision="21" />
</key>
<one-to-many class="XML_House"/>
</set>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_House" table="H_HOUSE_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">H_HOUSE_XML_SEQ</param>
</generator>
</id>
<property name="position"></property>
</class>
</hibernate-mapping>
8.1.1 类图
图8-1 类关联关系图
8.1.2 数据库表结构
图8-2 数据库表结构图
8.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
@Entity(name="H_HOUSE_")
@SequenceGenerator(name="HouseGene", sequenceName="H_HOUSE_SEQ", allocationSize=1)
public class House {
private BigDecimal id;
private String position;
private Person person;
@Id
@Column(precision=20)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="HouseGene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
@ManyToOne//如果需要指定外键字段使用@JoinColumn(name="PERSON_ID")
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
@Entity(name="H_PERSON_")
@SequenceGenerator(name="PersonGene", sequenceName="H_PERSON_SEQ", allocationSize=1)
public class Person {
private BigDecimal Id;
private String name;
private Set<House> houses = new HashSet<House>();
@Id
@Column(precision=21)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="PersonGene")
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@OneToMany(mappedBy="person")//参考了对方的属性名
public Set<House> getHouses() {
return houses;
}
public void setHouses(Set<House> houses) {
this.houses = houses;
}
}
8.2.1 类图
图8-3 类关联关系图
8.2.2 数据库表结构
图8-4 数据库表结构图
8.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_House {
private BigDecimal id;
private String position;
private XML_Person person;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public XML_Person getPerson() {
return person;
}
public void setPerson(XML_Person person) {
this.person = person;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class XML_Person {
private BigDecimal Id;
private String name;
private Set<XML_House> houses = new HashSet<XML_House>();
public BigDecimal getId() {
return Id;
}
public void setId(BigDecimal id) {
Id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<XML_House> getHouses() {
return houses;
}
public void setHouses(Set<XML_House> houses) {
this.houses = houses;
}
}
8.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Person" table="H_PERSON_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="21" scale="0" />
<generator class="sequence">
<param name="sequence">H_XML_PERSON_SEQ</param>
</generator>
</id>
<property name="name"/>
<set name="houses">
<key>
<!—关联表中指向当前类对应表的外键字段名,也可以<key column="PERSON_ID" /> -->
<column name="PERSON_ID" precision="21" />
</key>
<one-to-many class="XML_House"/>
</set>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_House" table="H_HOUSE_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">H_HOUSE_XML_SEQ</param>
</generator>
</id>
<property name="position"/>
<many-to-one name="person" column="PERSON_ID"/>
<!-- 字段名与对应的XML_Person.hbm.xml中配置的set的key中column的值必须一致 -->
</class>
</hibernate-mapping>
9.1.1 类图
图9-1 类关联关系图
9.1.2 数据库表结构
图9-2 数据库表结构图
9.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="T")//name是用来在HQL语句中应用的本类的名字
@Table(name="H_TEACHER_")//数据库中当前类对应的表名
@SequenceGenerator(name="Teacher_Gene", sequenceName="H_TEACHER_SEQ", allocationSize=1)
public class Teacher {
private BigDecimal id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Teacher_Gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany
@JoinTable(
name="H_T_S",//指明中间表的名字
joinColumns={@JoinColumn(name="TEACHER_ID", referencedColumnName="ID")},
//指明中间表参考当前类对应数据库表中的字段ID的外键名为TEACHER_ID
inverseJoinColumns={@JoinColumn(name="STUDENT_ID",referencedColumnName="ID")}
//指明中间表参考另外一张表中的字段ID的外键名为STUDENT_ID
)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="S")
@Table(name="H_STUDENT_")
@SequenceGenerator(name="Student_Gene", sequenceName="H_STUDENT_SEQ", allocationSize=1)
public class Student {
private BigDecimal id;
private String name;
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Student_Gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
9.2.1 类图
图8-3 类关联关系图
9.2.2 数据库表结构
图8-4 数据库表结构图
9.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class XML_Teacher {
private BigDecimal id;
private String name;
private Set<XML_Student> students = new HashSet<XML_Student>();
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<XML_Student> getStudents() {
return students;
}
public void setStudents(Set<XML_Student> students) {
this.students = students;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
public class XML_Student {
private BigDecimal id;
private String name;
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
9.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Teacher" table="H_TEACHER_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">H_TEACHER_XML_SEQ</param>
</generator>
</id>
<property name="name"/>
<set name="students" table="H_XML_T_S">
<key column="TEACHER_ID"/>
<!--中间表中指向当前类对应表的外键字段名为TEACHER_ID -->
<many-to-many class="XML_Student" column="SUTDNET_ID"/>
<!-- 中间表中指向XML_Student类对应表的外键字段名为STUDENT_ID -->
</set>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Student" table="H_STUDENT_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="21" scale="0" />
<generator class="sequence">
<param name="sequence">H_XML_STUDENT_SEQ</param>
</generator>
</id>
<property name="name"/>
</class>
</hibernate-mapping>
10.1.1 类图
图10-1 类关联关系图
10.1.2 数据库表结构
图10-2 数据库表结构图
10.1.3 实体类
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="S")
@Table(name="H_STUDENT_")
@SequenceGenerator(name="Student_Gene", sequenceName="H_STUDENT_SEQ", allocationSize=1)
public class Student {
private BigDecimal id;
private String name;
private Set<Teacher> teachers = new HashSet<Teacher>();
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Student_Gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany(mappedBy="students")
public Set<Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<Teacher> teachers) {
this.teachers = teachers;
}
}
package com.rongqq.hibernate3.annotation.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity(name="T")
@Table(name="H_TEACHER_")
@SequenceGenerator(name="Teacher_Gene", sequenceName="H_TEACHER_SEQ", allocationSize=1)
public class Teacher {
private BigDecimal id;
private String name;
private Set<Student> students = new HashSet<Student>();
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="Teacher_Gene")
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@ManyToMany
@JoinTable(
name="H_T_S",//指明中间表的名字
joinColumns={@JoinColumn(name="TEACHER_ID",
referencedColumnName="ID")},
//指明中间表参考当前类对应数据库表中的字段ID的外键名为TEACHER_ID
inverseJoinColumns={@JoinColumn(name="STUDENT_ID",
referencedColumnName="ID")}
//指明中间表参考另外一张表中的字段ID的外键名为STUDENT_ID
)
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
10.2.1 类图
图10-3 类关联关系图
10.2.2 数据库表结构
图10-4 数据库表结构图
10.2.3 实体类
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class XML_Teacher {
private BigDecimal id;
private String name;
private Set<XML_Student> students = new HashSet<XML_Student>();
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<XML_Student> getStudents() {
return students;
}
public void setStudents(Set<XML_Student> students) {
this.students = students;
}
}
package com.rongqq.hibernate3.xml.entity;
import java.math.BigDecimal;
import java.util.HashSet;
import java.util.Set;
public class XML_Student {
private BigDecimal id;
private String name;
private Set<XML_Teacher> teachers = new HashSet<XML_Teacher>();
public BigDecimal getId() {
return id;
}
public void setId(BigDecimal id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<XML_Teacher> getTeachers() {
return teachers;
}
public void setTeachers(Set<XML_Teacher> teachers) {
this.teachers = teachers;
}
}
10.2.4 对象映射文件
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Teacher" table="H_TEACHER_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="20" scale="0" />
<generator class="sequence">
<param name="sequence">H_TEACHER_XML_SEQ</param>
</generator>
</id>
<property name="name"/>
<set name="students" table="H_XML_T_S">
<key column="TEACHER_ID"/>
<!-- 中间表中指向当前类对应表的外键字段名为TEACHER_ID -->
<many-to-many class="XML_Student" column="STUDENT_ID"/>
<!-- 中间表中指向XML_Student类对应表的外键字段名为STUDENT_ID -->
</set>
</class>
</hibernate-mapping>
<?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="com.rongqq.hibernate3.xml.entity">
<class name="XML_Student" table="H_STUDENT_XML" dynamic-update="true">
<id name="id" type="big_decimal">
<column name="ID" precision="21" scale="0" />
<generator class="sequence">
<param name="sequence">H_XML_STUDENT_SEQ</param>
</generator>
</id>
<property name="name"/>
<set name="teachers" table="H_XML_T_S">
<!-- 相关联的类的映射文件两侧定义的中间表名字必须一致 -->
<key column="STUDENT_ID" />
<!-- 中间表中指向当前类对应表的外键字段名 -->
<many-to-many class="XML_Teacher" column="TEACHER_ID" />
<!-- 中间表中指向与当前类关联的另外一个类所对应表的外键字段名 -->
</set>
</class>
</hibernate-mapping>