- 浏览: 203604 次
- 性别:
- 来自: 芜湖
文章分类
- 全部博客 (139)
- 软件 (0)
- Pattern (6)
- CSDN导入 (19)
- Struts (3)
- [网站分类]1.网站首页原创 (27)
- [网站分类]6.转载区 (4)
- Hibernate (10)
- Error (8)
- [网站分类]2.Java新手区 (20)
- Java (8)
- [网站分类]4.其他技术区 (10)
- Web (1)
- C++ (2)
- Algorithm (4)
- Linux (2)
- Skill (1)
- Tech (2)
- Note (2)
- [网站分类]3.非技术区 (1)
- Database (1)
- Winty (7)
- [网站分类]1.网站首页原创Java技术区(对首页文章的要求: 原创、高质量、经过认真思考并精心写作。BlogJava管理团队会对首页的文章进行管理。) (0)
最新评论
-
haohao-xuexi02:
很不错哦。
O'Reilly cos上传组件的使用(1/3) - 上传文件 -
yoin528:
useUnicode=true&charact ...
[原]向MySQL数据库插入Blob数据的问题 -
xiaoqing20:
下载来看看!呵呵
[原]Struts2类型转换 -
xiaoqing20:
[原]Struts2类型转换
[标题]:[原]Hibernate多对多(单向)
[时间]:2009-6-19
[摘要]:Hibernate单向多对多关联。如:一个学生对应多门课程,一门课程也对应多名学生。
[关键字]:Hibernate,ORM,关联,多对多,持久化,映射
[环境]:MyEclipse7,Hibernate3.2,MySQL5.1
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
Hibernate单向多对多关联。如:一个学生对应多门课程,一门课程也对应多名学生。本例单向关联,只考虑学生到课程的一对多关联。
1、概述
a.实体类
public class Student{
......
private Set<Course> courses;
......
}
public class Course{
......
}
b.数据库表
Student与Course各对应一张数据库表,再建一张关联表student_course (studentid,courseid),保存多对多关联。其中,student_course表的主键为studentid与courseid的联合。
c.配置文件
Student.hbm.xml:
......
<set name="courses" table="student_course" cascade="all">
<key column="studentid" />
<many-to-many column="courseid" class="wintys.hibernate.manytomany.Course"/>
</set>
......
Course.hbm.xml:
......
2、实体类:
Student.java:
Course.java:
3、数据库表:
db.sql:
CREATE TABLE student(
id int(4) NOT NULL UNIQUE,
name varchar(100),
PRIMARY KEY(id)
);
CREATE TABLE course(
id int(4) NOT NULL UNIQUE,
name varchar(100),
PRIMARY KEY(id)
);
-- 关联表
CREATE TABLE student_course(
studentid int(4) NOT NULL,
courseid int(4) NOT NULL,
PRIMARY KEY(studentid,courseid),
CONSTRAINT FK_studentid FOREIGN KEY(studentid) REFERENCES student(id),
CONSTRAINT FK_courseid FOREIGN KEY(courseid) REFERENCES course(id)
);
4、映射文件:
Student.hbm.xml:
Course.hbm.xml:
hibernate.cfg.xml:
4、使用测试:
DAO.java:
DAOBean.java:
HibernateUtil.java:
index.jsp:查询Student及其课程列表
5、运行结果
控制台显示:
Web页面显示:
id:1
name:Student 1
courses:
id:1
name:Course 1
id:2
name:Course 2
--------------------------------------------------------------------------------
id:2
name:Student 2
courses:
id:3
name:Course 3
id:2
name:Course 2
--------------------------------------------------------------------------------
[参考资料]:
Hibernate 多对多单向关联-熔岩 : http://lavasoft.blog.51cto.com/62575/39324
[时间]:2009-6-19
[摘要]:Hibernate单向多对多关联。如:一个学生对应多门课程,一门课程也对应多名学生。
[关键字]:Hibernate,ORM,关联,多对多,持久化,映射
[环境]:MyEclipse7,Hibernate3.2,MySQL5.1
[作者]:Winty (wintys@gmail.com) http://www.blogjava.net/wintys
[正文]:
Hibernate单向多对多关联。如:一个学生对应多门课程,一门课程也对应多名学生。本例单向关联,只考虑学生到课程的一对多关联。
1、概述
a.实体类
public class Student{
......
private Set<Course> courses;
......
}
public class Course{
......
}
b.数据库表
Student与Course各对应一张数据库表,再建一张关联表student_course (studentid,courseid),保存多对多关联。其中,student_course表的主键为studentid与courseid的联合。
c.配置文件
Student.hbm.xml:
......
<set name="courses" table="student_course" cascade="all">
<key column="studentid" />
<many-to-many column="courseid" class="wintys.hibernate.manytomany.Course"/>
</set>
......
Course.hbm.xml:
......
2、实体类:
Student.java:
package wintys.hibernate.manytomany;
import java.util.Set;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class Student {
private Integer id;
private String name;
private Set<Course> courses;
public Student(){
}
public Student(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
import java.util.Set;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class Student {
private Integer id;
private String name;
private Set<Course> courses;
public Student(){
}
public Student(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Course> getCourses() {
return courses;
}
public void setCourses(Set<Course> courses) {
this.courses = courses;
}
}
Course.java:
package wintys.hibernate.manytomany;
import java.util.Set;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class Course {
private Integer id;
private String name;
public Course(){
}
public Course(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.util.Set;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class Course {
private Integer id;
private String name;
public Course(){
}
public Course(String name){
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3、数据库表:
db.sql:
CREATE TABLE student(
id int(4) NOT NULL UNIQUE,
name varchar(100),
PRIMARY KEY(id)
);
CREATE TABLE course(
id int(4) NOT NULL UNIQUE,
name varchar(100),
PRIMARY KEY(id)
);
-- 关联表
CREATE TABLE student_course(
studentid int(4) NOT NULL,
courseid int(4) NOT NULL,
PRIMARY KEY(studentid,courseid),
CONSTRAINT FK_studentid FOREIGN KEY(studentid) REFERENCES student(id),
CONSTRAINT FK_courseid FOREIGN KEY(courseid) REFERENCES course(id)
);
4、映射文件:
Student.hbm.xml:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="wintys.hibernate.manytomany.Student" table="student" catalog="db">
<id name="id" type="int">
<column name="id" not-null="true"/>
<generator class="increment" />
</id>
<property name="name" />
<set name="courses" table="student_course" cascade="all">
<key column="studentid" />
<many-to-many column="courseid" class="wintys.hibernate.manytomany.Course"/>
</set>
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="wintys.hibernate.manytomany.Student" table="student" catalog="db">
<id name="id" type="int">
<column name="id" not-null="true"/>
<generator class="increment" />
</id>
<property name="name" />
<set name="courses" table="student_course" cascade="all">
<key column="studentid" />
<many-to-many column="courseid" class="wintys.hibernate.manytomany.Course"/>
</set>
</class>
</hibernate-mapping>
Course.hbm.xml:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="wintys.hibernate.manytomany.Course" table="course" catalog="db">
<id name="id" type="int">
<column name="id" not-null="true"/>
<generator class="increment" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="wintys.hibernate.manytomany.Course" table="course" catalog="db">
<id name="id" type="int">
<column name="id" not-null="true"/>
<generator class="increment" />
</id>
<property name="name" />
</class>
</hibernate-mapping>
hibernate.cfg.xml:
<?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">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">MySQLDriver</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="wintys/hibernate/manytomany/Student.hbm.xml" />
<mapping resource="wintys/hibernate/manytomany/Course.hbm.xml" />
</session-factory>
</hibernate-configuration>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=utf-8
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">MySQLDriver</property>
<property name="connection.password">root</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="show_sql">true</property>
<mapping resource="wintys/hibernate/manytomany/Student.hbm.xml" />
<mapping resource="wintys/hibernate/manytomany/Course.hbm.xml" />
</session-factory>
</hibernate-configuration>
4、使用测试:
DAO.java:
package wintys.hibernate.manytomany;
import java.util.List;
public interface DAO {
public void insert();
public <T> List<T> selectAll(String hql);
}
import java.util.List;
public interface DAO {
public void insert();
public <T> List<T> selectAll(String hql);
}
DAOBean.java:
package wintys.hibernate.manytomany;
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class DAOBean implements DAO {
public void insert() {
Transaction tc = null;
try{
Course c1,c2,c3;
Student s1,s2;
Set<Course> cs1 , cs2;
c1 = new Course("Course 1");
c2 = new Course("Course 2");
c3 = new Course("Course 3");
s1 = new Student("Student 1");
s2 = new Student("Student 2");
cs1 = new HashSet<Course>();
cs2 = new HashSet<Course>();
//c2为两个集合共有
cs1.add(c1);
cs1.add(c2);
cs2.add(c2);
cs2.add(c3);
s1.setCourses(cs1);
s2.setCourses(cs2);
Session session = HibernateUtil.getSession();
tc = session.beginTransaction();
/*
在Student.hbm.xml中设置了cascade="all",就不需要手动保存c1/c2/c3
session.save(c1);
session.save(c2);
session.save(c3);
*/
session.save(s1);
session.save(s2);
tc.commit();
}catch(HibernateException e){
try{
if(tc != null)
tc.rollback();
}catch(Exception ex){
System.err.println(ex.getMessage());
}
System.err.println(e.getMessage());
}finally{
HibernateUtil.closeSession();
}
}
@SuppressWarnings("unchecked")
public <T> List<T> selectAll(String hql) {
List<T> items = null;
Transaction tc = null;
try{
Session session = HibernateUtil.getSession();
tc = session.beginTransaction();
Query query = session.createQuery(hql);
items = query.list();
tc.commit();
}catch(HibernateException e){
try{
if(tc != null){
tc.rollback();
items = null;
}
}catch(Exception ex){
System.err.println(ex.getMessage());
}
System.err.println(e.getMessage());
}finally{
//HibernateUtil.closeSession();
}
return items;
}
}
import java.util.List;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
/**
* @version 2009-06-19
* @author Winty (wintys@gmail.com)
*
*/
public class DAOBean implements DAO {
public void insert() {
Transaction tc = null;
try{
Course c1,c2,c3;
Student s1,s2;
Set<Course> cs1 , cs2;
c1 = new Course("Course 1");
c2 = new Course("Course 2");
c3 = new Course("Course 3");
s1 = new Student("Student 1");
s2 = new Student("Student 2");
cs1 = new HashSet<Course>();
cs2 = new HashSet<Course>();
//c2为两个集合共有
cs1.add(c1);
cs1.add(c2);
cs2.add(c2);
cs2.add(c3);
s1.setCourses(cs1);
s2.setCourses(cs2);
Session session = HibernateUtil.getSession();
tc = session.beginTransaction();
/*
在Student.hbm.xml中设置了cascade="all",就不需要手动保存c1/c2/c3
session.save(c1);
session.save(c2);
session.save(c3);
*/
session.save(s1);
session.save(s2);
tc.commit();
}catch(HibernateException e){
try{
if(tc != null)
tc.rollback();
}catch(Exception ex){
System.err.println(ex.getMessage());
}
System.err.println(e.getMessage());
}finally{
HibernateUtil.closeSession();
}
}
@SuppressWarnings("unchecked")
public <T> List<T> selectAll(String hql) {
List<T> items = null;
Transaction tc = null;
try{
Session session = HibernateUtil.getSession();
tc = session.beginTransaction();
Query query = session.createQuery(hql);
items = query.list();
tc.commit();
}catch(HibernateException e){
try{
if(tc != null){
tc.rollback();
items = null;
}
}catch(Exception ex){
System.err.println(ex.getMessage());
}
System.err.println(e.getMessage());
}finally{
//HibernateUtil.closeSession();
}
return items;
}
}
HibernateUtil.java:
package wintys.hibernate.manytomany;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate Session管理
* @author Winty
*/
public class HibernateUtil {
private static SessionFactory factory = null;
private static ThreadLocal<Session> threadLocal;
static {
try{
factory = new Configuration()
.configure()
.buildSessionFactory();
}catch(HibernateException e){
System.err.println(e.getMessage());
}
threadLocal = new ThreadLocal<Session>();
}
private HibernateUtil(){
}
public static Session getSession()throws HibernateException{
Session session = threadLocal.get();
if(session == null){
session = factory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession()throws HibernateException{
Session session = threadLocal.get();
if(session != null){
session.close();
}
threadLocal.set(null);
}
}
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Hibernate Session管理
* @author Winty
*/
public class HibernateUtil {
private static SessionFactory factory = null;
private static ThreadLocal<Session> threadLocal;
static {
try{
factory = new Configuration()
.configure()
.buildSessionFactory();
}catch(HibernateException e){
System.err.println(e.getMessage());
}
threadLocal = new ThreadLocal<Session>();
}
private HibernateUtil(){
}
public static Session getSession()throws HibernateException{
Session session = threadLocal.get();
if(session == null){
session = factory.openSession();
threadLocal.set(session);
}
return session;
}
public static void closeSession()throws HibernateException{
Session session = threadLocal.get();
if(session != null){
session.close();
}
threadLocal.set(null);
}
}
index.jsp:查询Student及其课程列表
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="wintys.hibernate.manytomany.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%
List<Student> students = null;
DAO dao = new DAOBean();
dao.insert();
students = dao.selectAll("from Student");
Iterator<Student> it = students.iterator();
while(it.hasNext()){
Student student = it.next();
int id = student.getId();
String name = student.getName();
out.println("id:" + id + "<br />");
out.println("name:" + name + "<br />");
out.println("courses:<br />");
Set<Course> cards = student.getCourses();
Iterator<Course> itc = cards.iterator();
while(itc.hasNext()){
Course course = itc.next();
int courseId = course.getId();
String courseName = course.getName();
out.println(" id:" + courseId + "<br />");
out.println(" name:" + courseName + "<br />");
}
out.println("<hr/>");
}
%>
</body>
</html>
<%@ page import="wintys.hibernate.manytomany.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
</head>
<body>
<%
List<Student> students = null;
DAO dao = new DAOBean();
dao.insert();
students = dao.selectAll("from Student");
Iterator<Student> it = students.iterator();
while(it.hasNext()){
Student student = it.next();
int id = student.getId();
String name = student.getName();
out.println("id:" + id + "<br />");
out.println("name:" + name + "<br />");
out.println("courses:<br />");
Set<Course> cards = student.getCourses();
Iterator<Course> itc = cards.iterator();
while(itc.hasNext()){
Course course = itc.next();
int courseId = course.getId();
String courseName = course.getName();
out.println(" id:" + courseId + "<br />");
out.println(" name:" + courseName + "<br />");
}
out.println("<hr/>");
}
%>
</body>
</html>
5、运行结果
控制台显示:
......
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.student (name, id) values (?, ?)
Hibernate: insert into db.student (name, id) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
......
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.course (name, id) values (?, ?)
Hibernate: insert into db.student (name, id) values (?, ?)
Hibernate: insert into db.student (name, id) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
Hibernate: insert into student_course (studentid, courseid) values (?, ?)
......
Web页面显示:
id:1
name:Student 1
courses:
id:1
name:Course 1
id:2
name:Course 2
--------------------------------------------------------------------------------
id:2
name:Student 2
courses:
id:3
name:Course 3
id:2
name:Course 2
--------------------------------------------------------------------------------
[参考资料]:
Hibernate 多对多单向关联-熔岩 : http://lavasoft.blog.51cto.com/62575/39324
原创作品,转载请注明出处。
作者:Winty (wintys@gmail.com)
博客:http://www.blogjava.net/wintys
作者:Winty (wintys@gmail.com)
博客:http://www.blogjava.net/wintys
发表评论
-
Hibernate一对多(单向)
2009-07-14 23:01 1398[原]Hibernate一对多(单向) ... -
Hibernate继承映射-继承关系中每个类均映射为一个数据库表
2009-07-14 22:49 1745[标题]:[原]Hibernate继承映射-继承关系中每个类均 ... -
[原]Hibernate一对多(双向)
2009-06-17 22:04 1931[标题]:Hibernate一对多(双向) [时间]:2009 ... -
[原]在MyEclipse中开发Hibernate入门
2009-06-17 22:21 927[标题]:在MyEclipse中开发Hibernate入门 ... -
[原]Hibernate多对多(双向)
2009-06-19 22:50 1107[标题]:[原]Hibernate多对多(双向) [时间]: ... -
[原]Hibernate继承映射-具体类映射为数据库表
2009-06-21 13:05 1079[标题]:[原]Hibernate继承映射-具体类映射为数据库 ... -
[原]Hibernate继承映射-整个类层次映射为单个数据库表
2009-06-21 13:17 828[标题]:[原]Hibernate继承映射-整个类层次映射为单 ... -
Hibernate继承映射-概述
2009-06-21 14:24 1073Hibernate中对继承关系的 ... -
[原]Hibernate集合映射
2009-07-03 23:50 1469[标题]:[原]Hibernate集合映射 [时间]:200 ...
相关推荐
本项目“Hibernate学习:单向多对一关联 工程”专注于讲解Hibernate中的单向多对一关联映射,这是数据库设计中常见的关系类型,尤其在处理具有层次结构的数据时。 单向多对一关联指的是在一个实体类中有一个引用,...
// @ManyToOne注解表示这个字段是一个多对一的关系,对应Parent实体 // cascade属性可以设置成CascadeType.ALL,以处理级联操作,如保存、删除等 @ManyToOne(cascade = CascadeType.ALL) private Parent parent;...
双向映射可以通过多对一、一对一、一对多关系来实现。 控件映射 Hibernate 的控件映射可以将一个对象的控件属性映射到数据库中。控件映射可以使用 Map 实现动态控件。 继承映射 Hibernate 的继承映射可以将一个...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
- **多对一(many-to-one)**: 实体间的多对一关系。 - **一对一(one-to-one)**: 实体间的一对一关系。 #### 7. 组件映射 - **组件映射**: 将Java对象中的某个属性映射为数据库表中的多个字段。 #### 8. 继承...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 ...
一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
8.4.1. 一对多(one to many) / 多对一(many to one) 8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many ...
对于多对一关系,如果总是成对出现,可以考虑使用联合主键或单向关联。 - 使用二级缓存:通过配置Hibernate的二级缓存,可以减少对数据库的访问,提高系统性能。 - HQL与SQL:使用HQL(Hibernate Query Language)...
- **7.5.1 一对多 (one to many)/多对一 (many to one)**:说明使用连接表的双向一对多/多对一关联映射。 - **7.5.2 一对一 (one to one)**:介绍使用连接表的双向一对一关联映射。 - **7.5.3 多对多 (many-to-...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
3. **多对多(Many-to-Many)**:如部门表和人员表之间的关系。 ##### 对象表示 在 Java 对象中,这些关系可以通过不同的方式来表示: 1. **一对一(One-to-One)**: - 单向关系:例如,`Department` 类包含一个...
- 使用`@ManyToOne`注解进行多对一映射配置。 - **一对多映射**: - 使用`@OneToMany`注解进行一对多映射配置。 - **多对多映射**: - 使用`@ManyToMany`注解进行多对多映射配置。 - **次级表映射**: - 使用`...
关系的数据库表示可以分为一对一、一对多和多对多三种类型。例如,一个部门表和一个部门主管表之间是一对一关系,而一个部门表和下属人员表之间则是一对多关系。对象表示方面,对象关系分为单向和双向。双向关系意味...
- **单向双联**:一方维护关系,另一方不维护,常见于一对多或多对一关系。 - **继承映射策略**:支持多种继承映射策略,如`joined`(联合表)、`single_table`(单表)等。 #### Hibernate对象状态 - **瞬时态**:...