一对一
一对一主键关联
1.XML配置
- 必须双向关联
- 被关联方主键生成策略为外键生成策略
- 被关联方one-to-one需设置constrained=“true”约束
<!--Person表映射-->
<?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="com.kaishengit.pojo">
<class name="Person" table="person">
<id column="id" name="id">
<generator class="native"/>
</id>
<property name="personName" column="person_name"/>
<one-to-one name="card" cascade="all"/>
</class>
</hibernate-mapping>
<!--Card表映射-->
<?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="com.kaishengit.pojo">
<class name="Card" table="card">
<id column="id" name="id">
<generator class="foreign">
<param name="property">person</param>
</generator>
</id>
<property name="cardNum" column="card_num"/>
<one-to-one name="person" constrained="true"/>
</class>
</hibernate-mapping>
2.Java注解配置
- 主控方使用@PrimaryKeyJoinColumn
- 被控方在@OneToOne中要加上(mappedBy = "card")
//基于注解的配置
@Entity
@Table(name = "person")
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "person_name")
private String personName;
//共享主键关联使用PrimaryKeyJoinColumn
@OneToOne(cascade = CascadeType.ALL)
@PrimaryKeyJoinColumn
private Card card;
}
@Entity
@Table(name = "card")
public class Card {
@Id
@GenericGenerator(name = "generator",strategy = "foreign",
parameters = @org.hibernate.annotations.Parameter(name = "property",value = "person"))
@GeneratedValue(generator = "generator")
private Integer id;
@Column(name = "card_num")
private String cardNum;
//必须设置OneToOne,mappedBy为自己在对方中的属性名
@OneToOne(mappedBy = "card")
private Person person;
}
一对一外键关联
数据库结构
1.XML配置
- 各自主键生成策略无外键约束
- 主控方(含外键的一方)使用many-to-one,并且要加上unique="true"约束
- 被控方使用one-to-one,并且要加上property-ref="passageContent"
<!--Passage表-->
<?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="com.kaishengit.pojo">
<class name="Passage" table="passage">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="title" column="title"/>
<many-to-one name="passageContent" column="content_id" class="PassageContent" unique="true"/>
</class>
</hibernate-mapping>
<!--PassageContent表-->
<?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="com.kaishengit.pojo">
<class name="PassageContent" table="passage_content">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="content" column="content"/>
<one-to-one name="passage" class="Passage" property-ref="passageContent"/>
</class>
</hibernate-mapping>
2.Java注解配置
- 区别于XML配置,这里全部采用@OneToOne
- 主控方使用@JoinColumn(name = "content_id")
- 被控方使用@OneToOne(mappedBy = "passageContent"),mappedBy相当于XML中的property-ref,是一个反向声明
@Entity
@Table(name = "passage")
public class Passage {
@Id
@GeneratedValue
private Integer id;
private String title;
@OneToOne
@JoinColumn(name = "content_id")
private PassageContent passageContent;
}
@Entity
@Table(name = "passage_content")
public class PassageContent {
@Id
@GeneratedValue
private Integer id;
private String content;
@OneToOne(mappedBy = "passageContent")
private Passage passage;
}
一对多关联
-
保存时先保存一端,再保存多端
数据库结构
1.XML配置
- 一端在set中要指明表及关联的键(即table和key属性),并且要放弃关系维护
<!--Address表-->
<?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="com.kaishengit.pojo">
<class name="Address" table="address">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="cityName" column="city_name"/>
<property name="address" column="address"/>
<!--
多对一
name: 属性名称
class: 属性的类型
column: 外键的名称
lazy: false表示关闭延时加载
fetch: join 使用连接查询对象
-->
<many-to-one name="user" class="User" column="user_id" lazy="false" fetch="join"></many-to-one>
</class>
</hibernate-mapping>
<!--User表-->
<?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="com.kaishengit.pojo">
<class name="User" table="user">
<cache usage="read-only"/>
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="username" column="username"/>
<set name="addressSet" table="address" cascade="delete" inverse="true">
<key column="user_id"></key>
<one-to-many class="Address"></one-to-many>
</set>
</class>
</hibernate-mapping>
2.Java注解配置
- ManyToOne端要加上JoinColumn,OneToMany要加上mappedBy
- 如需级联操作,一端还需加上Cascade
@Entity
@Table
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "city_name")
private String cityName;
private String address;
@ManyToOne
@JoinColumn(name = "user_id")
private User user;
}
@Entity
@Table
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String username;
@OneToMany(mappedBy = "user")
@Cascade(CascadeType.DELETE)
private Set<Address> addressSet;
}
多对多关联
数据库结构
1.XML配置
<!--Student表-->
<?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="com.kaishengit.pojo">
<class name="Student" table="student">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="studentName" column="student_name"/>
<set name="teacherSet" table="student_teacher_real">
<key column="student_id"></key>
<many-to-many class="Teacher" column="teacher_id"/>
</set>
</class>
</hibernate-mapping>
<!--Teacher表-->
<?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="com.kaishengit.pojo">
<class name="Teacher" table="teacher">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="teacherName" column="teacher_name"/>
<set name="studentSet" table="student_teacher_real" inverse="true">
<key column="teacher_id"></key>
<many-to-many class="Student" column="student_id"/>
</set>
</class>
</hibernate-mapping>
2.Java注解配置
@Entity
@Table
public class Student {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "student_name")
private String studentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@ManyToMany
@JoinTable(name = "student_teacher_real",joinColumns = {@JoinColumn(name = "student_id")},inverseJoinColumns = {@JoinColumn(name = "teacher_id")})
private Set<Teacher> teacherSet;
}
@Entity
@Table
public class Teacher {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@Column(name = "teacher_name")
private String teacherName;
@ManyToMany(mappedBy = "teacherSet")
private Set<Student> studentSet;
}
Java注解总结
- 如果是主键关联,那么就使用@PrimaryKeyJoinColumn,只要声明@JoinColumn,@PrimaryKeyJoinColumn,@JoinTable的都属于主控方,外键通常也就存储在主控方,对应的含有mappedBy的都为被控方。
- 一对一关联不管是主键关联还是外键关联,基于注解都是OneToOne
XML配置总结
- 一对一主键关联时,由于被控方的主键是主控方的外键,所以被控方的OneToOne标签中需要加constrained="true"约束
- 被控方通常需要设置inverse="true"放弃关系维护
- <one-to-many><many-to-many>这样的标签放置在<set>集合内部