- 浏览: 23078 次
- 性别:
- 来自: 厦门
最新评论
(摘自互联网,备份)Hibernate实体关系:双向关联,mappedBy必设
注解方式
1、一对一单向
@Entity public class Husband{ @Id @GenerateValue pirvate int id; private String name; @OneToOne @JoinColumn(name="wifeId") private Wife wife; } @Entity public class Wife{ @Id @GenerateValue private int id; private String name; }
2.、一对一双向
@Entity public class Husband{ @Id @GenerateValue pirvate int id; private String name; @OneToOne @JoinColumn(name="wifeId") private Wife wife; } @Entity public class Wife{ @Id @GenerateValue private int id; private String name; @OneToOne(mappedBy="wife") private Husband husband; }
3.、一对多单向
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @Id @GeneratedValue 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; } @OneToMany @JoinColumn(name="groupId") //不加出现3张表 public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } } package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private int id; private String name; @Id @GeneratedValue 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; } }
4、多对一单向
package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; @Id @GeneratedValue 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; } } package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private int id; private String name; private Group group; @ManyToOne public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } @Id @GeneratedValue 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; } }
5、一对多双向
package com.bjsxt.hibernate; import java.util.HashSet; import java.util.Set; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="t_group") public class Group { private int id; private String name; private Set<User> users = new HashSet<User>(); @Id @GeneratedValue 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; } @OneToMany(mappedBy="group") public Set<User> getUsers() { return users; } public void setUsers(Set<User> users) { this.users = users; } } package com.bjsxt.hibernate; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="t_user") public class User { private int id; private String name; private Group group; @ManyToOne public Group getGroup() { return group; } public void setGroup(Group group) { this.group = group; } @Id @GeneratedValue 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; } }
6、多对多单向
@Entity public class Student { private int id; private String name; @Id @GeneratedValue 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; } } @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue 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; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) //改变中间表的名字和字段名字 public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
7、多对多双向
@Entity public class Student { private int id; private String name; private Set<Teacher> teachers = new HashSet<Teacher>(); @ManyToMany(mappedBy="students") public Set<Teacher> getTeachers() { return teachers; } public void setTeachers(Set<Teacher> teachers) { this.teachers = teachers; } @Id @GeneratedValue 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; } } @Entity public class Teacher { private int id; private String name; private Set<Student> students = new HashSet<Student>(); @Id @GeneratedValue 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; } @ManyToMany @JoinTable(name="t_s", joinColumns={@JoinColumn(name="teacher_id")}, inverseJoinColumns={@JoinColumn(name="student_id")} ) public Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } }
XML配置
1、一对一单向
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
2.、一对一双向
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" unique="true" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <one-to-one name="person" property-ref="address"/> </class>
3.、一对多单向
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses"> <key column="personId" not-null="true"/> <one-to-many class="Address"/> </set> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
4、多对一单向
<class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <many-to-one name="address" column="addressId" not-null="true"/> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> </class>
5、一对多双向
<hibernate-mapping> <class name="com.bjsxt.hibernate.Group" table="t_group"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="users" inverse=”true”> <key column="groupId"></key> //与<many-to-one>的column一致 <one-to-many class="com.bjsxt.hibernate.User"/> </set> </class> </hibernate-mapping> <hibernate-mapping> <class name="com.bjsxt.hibernate.User" table="t_user"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <many-to-one name="group" column="groupId"></many-to-one> </class> </hibernate-mapping>
6、多对多单向
<hibernate-mapping> <class name="com.bjsxt.hibernate.Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> </class> </hibernate-mapping> <hibernate-mapping> <class name="com.bjsxt.hibernate.Teacher"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="teacher_id"></key> <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/> </set> </class> </hibernate-mapping>
7、多对多双向
<hibernate-mapping> <class name="com.bjsxt.hibernate.Student"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="teachers" table="t_s" inverse=”true”> <key column="student_id"></key> <many-to-many class="com.bjsxt.hibernate.Teacher" column="teacher_id"/> </set> </class> </hibernate-mapping> <hibernate-mapping> <class name="com.bjsxt.hibernate.Teacher"> <id name="id"> <generator class="native"></generator> </id> <property name="name"></property> <set name="students" table="t_s"> <key column="teacher_id"></key> <many-to-many class="com.bjsxt.hibernate.Student" column="student_id"/> </set> </class> </hibernate-mapping>
发表评论
-
CXF simple frontend, allow all SSL certificates and set basic authentication cre
2011-09-26 11:53 1744(转自http://blog.progs.be/71/cxf- ... -
spring3+hibernate3.3.5出现的问题
2011-07-06 13:52 1499项目中使用spring3.0.3+hibernate3.3.5 ... -
illegal attempt to dereference collection
2010-10-26 08:58 2004使用中遇到此问题,网上搜索是版本的问题! 例如: from ... -
基于 Struts 2 拦截器实现细粒度的基于角色的存取控制(转)
2010-08-20 14:27 732本文介绍如何利用 Struts ... -
IntelliJ Cannot run web applications in Tomcat with port 8080
2009-08-13 23:07 1092杀毒软件是ESET NOD32,处理方法把ESET NOD32 ... -
在struts2中使用freemarker
2009-07-25 16:10 1598普通的struts2配置文件一 ... -
清理缓存--jsp
2009-04-24 17:55 1384response.setHeader("Pragma ...
相关推荐
本资源“Hibernate实体映射”提供了一个深入理解这一关键概念的机会,通过实例代码和教程笔记,帮助学习者更好地掌握Hibernate的核心功能。 一、Hibernate实体映射基础 在Hibernate中,实体映射是将Java对象模型与...
### Hibernate实体关联关系映射详解 #### 一、引言 Hibernate作为一种强大的对象关系映射(Object Relational Mapping,简称ORM)框架,在Java开发领域扮演着极其重要的角色。它不仅简化了持久化层的开发工作,...
### Hibernate实体映射文件字段设置默认值 在Hibernate框架中,实体映射文件(通常为`.hbm.xml`)用于定义Java对象与数据库表之间的映射关系。这其中包括了属性到数据库表列的映射、主键生成策略、以及一些高级特性...
本教程将带你入门Hibernate的多对多实体映射,帮助你理解如何通过源码来实现这种复杂的关系映射。 一、Hibernate概述 Hibernate是一个对象关系映射(ORM)框架,它允许我们将Java对象(实体)与数据库表进行映射,...
Hibernate实体关系映射
注:为节省空间,程序中需要的jar包,均在HibernateManytoManyMapCascadingSave.zip\HibernateManytoManyMapCascadingSave\lib\ <br>Hibernate 多对多实体映射实例 <br>学习Hibernate 实体映射的映射的好帮手...
`hibernate实体映射关系.doc`可能包含了详细讲解Hibernate实体映射关系的文档,涵盖XML和注解两种方式的具体配置,包括但不限于: - 实体类的创建与映射 - 主键生成策略的配置 - 不同关联关系(一对一、一对多、多...
"自动生成 Hibernate 映射文件和实体类" 自动生成 Hibernate 映射文件和实体类是使用 MyEclipse 工具来实现的,这可以大大提高开发效率,减少开发时间。下面将详细介绍如何使用 MyEclipse 自动生成 Hibernate 映射...
Hibernate 是一个流行的对象关系映射(ORM)框架,它允许开发者使用 Java 对象来操作数据库,消除了直接编写 SQL 的需要。在 Hibernate 中,映射关系是将数据库表与 Java 类之间的关联方式,使得对象模型可以与关系...
Hibernate实体关系映射 一对一关系 一对多关系 多对多关系Hibernate实体关系映射
总结,Hibernate实体层设计是Java开发中非常重要的一环,理解并熟练掌握实体的定义、属性映射和关联映射,以及如何在实际项目中运用,能够极大地提升开发效率和代码质量。在设计实体层时,应充分考虑业务需求,合理...
###Hibernate实体关联关系映射知识总结 #### 映射关系的类型 1. **一对一关联**: - **一对一外键关联**:一个实体的外键指向另一个实体的主键。 - **一对一主键关联**:两个实体共享同一个主键。 - **一对一...
Hibernate实体关联关系映射--学习总结.pdf
在IT行业中,数据库管理和对象关系映射(ORM)框架是至关重要的部分,特别是对于Java开发者而言,Hibernate是一个广泛使用的ORM工具。本主题将深入探讨Hibernate集合映射与关联关系,包括"student与Score的Map集合...
**hibernate实体生成工具**是开发者在使用Hibernate框架时常用的一种辅助工具,它能够自动生成与数据库表对应的Java实体类,大大节省了手动编写代码的时间,提高了开发效率。Hibernate是一个强大的对象关系映射(ORM...
总之,生成Hibernate实体类是Java开发中的常见任务,它依赖于正确的JDBC配置和映射文件。了解并熟练掌握这一过程对于有效地利用Hibernate框架至关重要。同时,保持对生成代码的审查和调整也是保证代码质量的重要环节...
### Hibernate实体类注解详解 Hibernate作为Java领域中流行的ORM(对象关系映射)框架,提供了丰富的注解来简化实体类与数据库表之间的映射。本文将深入解析一系列关键注解,帮助理解Hibernate如何实现这一过程。 ...