hibernate对象关系映射的配置
一对一主键关联单双向
一对一外键关联单双向
一对多/多对一
多对多单双向
Annotation注解(多对一)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wepull.pojo">
<class name="IdCard" table="t_idcard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 双向配置如下 -->
<one-to-one name="p"></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.pojo">
<class name="Person" table="t_person">
<id name="id">
<generator class="foreign">
<param name="property">i</param>
</generator>
</id>
<property name="name"></property>
<!-- person找Idcard 单向 -->
<one-to-one name="i" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
一对一外键关联单双向
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wepull.pojo">
<class name="IdCard" table="t_idcard">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 双向配置 级联删除-->
<one-to-one name="p" property-ref="i" cascade="delete"></one-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.pojo">
<class name="Person" table="t_person">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- person找Idcard 单向 -->
<many-to-one name="i" unique="true"></many-to-one>
</class>
</hibernate-mapping>
一对多/多对一
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.wepull.pojo">
<class name="Student" table="t_student">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 多对一配置如下 -->
<many-to-one name="c" class="Classes"></many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.pojo">
<class name="Classes" table="t_classes">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 一对多 -->
<set name="s" inverse="true"><!-- 由学生管理 -->
<!-- column:多的一方持有一的一方的外键字段 -->
<key column="c"></key>
<one-to-many class="Student" />
</set>
</class>
</hibernate-mapping>
多对多单双向
<hibernate-mapping package="com.wepull.pojo">
<class name="Role" table="t_role">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 多对多 -->
<set name="u" table="t_user_role">
<key column="r"></key>
<many-to-many class="User" column="u"></many-to-many>
</set>
</class>
</hibernate-mapping>
<hibernate-mapping package="com.wepull.pojo">
<class name="User" table="t_user">
<id name="id">
<generator class="native"></generator>
</id>
<property name="name"></property>
<!-- 多对多配置如下 -->
<set name="r" table="t_user_role">
<key column="u"></key>
<many-to-many class="Role" column="r"></many-to-many>
</set>
</class>
</hibernate-mapping>
Annotation注解(多对一)
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
/**
* @author fcs
* 不能自动删除表
* 多对一单向 多方持有一方的外键
* 2014-8-24
*/
@Entity
public class Student {
private int id;
private String name;
private Classes c;
@ManyToOne(fetch=FetchType.LAZY)//延迟加载
@JoinColumn(name="cid")//修改表字段名 防止自动生成sid
public Classes getC() {
return c;
}
public void setC(Classes c) {
this.c = c;
}
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)//主键自增长
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;
}
}
==================================
赵客缦胡缨,吴钩霜雪明。
银鞍照白马,飒沓如流星。
==================================