hibernate关联关系映射

    本文将介绍hibernate的几种关联关系映射:多对一、一对一、一对多、多对多。

多对一

以用户和组为例,假设多个用户对应一个组,用户即为多的一端,组即为一的一端。

关键代码和配置:

用户:

public class User implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private String name;
	private Group group;

	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;
	}

	public Group getGroup() {
		return group;
	}

	public void setGroup(Group group) {
		this.group = group;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po">
	<class name="User" table="t_user">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
		<!-- cascade="all" fetch="join" -->
		<many-to-one name="group" column="groupid" />
	</class>
</hibernate-mapping>

组:

public class Group implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private String name;

	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;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po">
	<class name="Group" table="t_group">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
	</class>
</hibernate-mapping>

一对一

以人和身份证号为例,一个人对应一个身份证号。

使用<many-to-one>实现一对一

关键代码和配置:

人:

public class Person implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private String name;
	private IdCard idCard;

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public IdCard getIdCard() {
		return idCard;
	}

	public void setIdCard(IdCard idCard) {
		this.idCard = idCard;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po.one2one">
	<class name="Person" table="t_person1">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
		<many-to-one name="idCard" column="cardid" />
	</class>
</hibernate-mapping>

身份证号:

public class IdCard implements Serializable {

	private static final long serialVersionUID = 1L;

	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po.one2one">
	<class name="IdCard" table="t_idcard">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
	</class>
</hibernate-mapping>

使用<one-to-one>实现一对一

代码可参考上一小节内容,这里只列出关键配置:

人:

<hibernate-mapping package="com.ado.test.hibernate.po.one2oneforeign">
	<class name="Person" table="t_person2">
		<id name="id" column="id" type="long">
			<generator class="foreign">
				<param name="property">idCard</param>
			</generator>
		</id>
		<property name="name" column="name" />
		<one-to-one name="idCard" />
	</class>
</hibernate-mapping>

身份证号:

<hibernate-mapping package="com.ado.test.hibernate.po.one2oneforeign">
	<class name="IdCard" table="t_idcard2">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
	</class>
</hibernate-mapping>

一对多

以班级和学生为例,一个班级有多个学生,即班级为一的一端,学生为多的一端。

关键代码和配置:

班级:

public class Classes implements Serializable {
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;

	private Set<Student> students;

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Set<Student> getStudents() {
		return students;
	}

	public void setStudents(Set<Student> students) {
		this.students = students;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po.one2many">
	<class name="Classes" table="t_class">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
		<set name="students" <span style="color:#ff0000;">inverse="true"</span>>
			<key column="classid" not-null="true" />
			<one-to-many class="Student" />
		</set>
	</class>
</hibernate-mapping>

注意:上面的配置中,增加了inverse="true"属性,可以避免保存Classes时,生成update语句维护映射关系(update t_student)。设置该属性后,保存数据时,要注意顺序,否则外键可能被设置空值。

学生:

public class Student implements Serializable {
	private static final long serialVersionUID = 1L;
	private Long id;
	private String name;

	public Long getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

}
<hibernate-mapping package="com.ado.test.hibernate.po.one2many">
	<class name="Student" table="t_student">
		<id name="id" column="id" type="long">
			<generator class="increment" />
		</id>
		<property name="name" column="name" />
	</class>
</hibernate-mapping>





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。