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;
}
}
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;
}
}
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>
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>
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>
4、使用测试:
DAO.java:
package wintys.hibernate.manytomany;
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;
}
}
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);
}
}
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>
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 (?, ?)
......
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
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;
}
}
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;
}
}
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>
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>
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>
4、使用测试:
DAO.java:
package wintys.hibernate.manytomany;
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;
}
}
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);
}
}
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>
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 (?, ?)
......
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
发表评论
-
spring + shiro
2012-07-24 11:20 011111 -
spring properties
2011-03-22 10:07 1718jingbo2759 的 spring 引用propertie ... -
spring的axis axis2 httpinvoker hessian cfx远程调用
2011-03-18 13:35 1569实体 package com.micc.entity; im ... -
hibernate cascade
2011-02-18 09:45 625Hibernate Cascade属性的用法 all :: ... -
jar命令
2010-12-23 09:35 604常常在网上看到有人询 ... -
dom
2010-10-20 10:12 718作者:康董 喜欢Web前端开发的,热爱javascript与 ... -
jvm参数
2010-08-27 17:18 1423jdk1.4.2 JVM官方地址:http://java.su ... -
hibernate any
2010-07-26 12:19 931Hibernate中any元素的应用 ... -
spring事务
2010-03-17 08:37 807根据代理机制不同,有以下几种配置方式: 先定义一个DAO ... -
hibernate中的update等方法为何除了object外还有一个entityName参数
2009-10-19 11:59 1723hibernate 3中的update方法有个重载 upda ... -
SimpleFormController处理流程
2009-09-22 18:10 1083SimpleFormController往上繼承自Abstra ... -
spring DispatcherServlet建立会话分析
2009-09-20 22:56 1620<servlet> <ser ... -
spring包详解
2009-09-16 16:06 955spring.jar是包含有完整发 ...
相关推荐
以上就是关于Hibernate单向多对多关联映射的基本介绍和实践。实际应用中,还需考虑性能优化、事务管理、懒加载等问题。通过不断的练习和学习,你可以更熟练地掌握这一重要功能,提升项目开发的效率和质量。
本主题聚焦于"hibernate单向多对多映射",特别是在XML配置文件中的实现。在这个过程中,我们将探讨多对多关系的原理,以及如何在Hibernate中通过XML配置文件来定义这种关系。 首先,我们需要理解多对多关系的概念。...
以下是对"hibernate单向多对多映射(注解版)"的详细解释。 首先,我们需要理解多对多关系的概念。在数据库设计中,多对多关系意味着一个实体可以与多个其他实体相关联,反之亦然。例如,学生和课程之间的关系就是多...
本项目“Hibernate学习:单向多对一关联 工程”专注于讲解Hibernate中的单向多对一关联映射,这是数据库设计中常见的关系类型,尤其在处理具有层次结构的数据时。 单向多对一关联指的是在一个实体类中有一个引用,...
对于单向多对一关联,通常是在多方(拥有多个实体)的类中添加一个对单方(拥有单一实体)的引用。 例如,我们可以创建两个实体类:`Employee`(员工)和`Department`(部门)。在`Employee`类中,我们将有一个`...
在Java的持久化框架Hibernate中,单向一对多关联映射是常见的数据关系处理方式,尤其是在处理数据库中的实体类和表之间的关系时。本主题主要关注如何使用注解来实现这种映射。Hibernate通过注解使得对象关系映射...
在本主题"Hibernate单向一对多关联映射(XML版)"中,我们将深入探讨如何使用XML配置来实现数据库中一个实体类对应多个实体类的关联关系。 在Hibernate中,一对多关联是一种常见的关系映射类型,它反映了数据库中的...
hibernate关联映射注解多对多单向关联、
本文将详细讨论在Hibernate 5中如何实现ManyToMany单向维护的多对多关联。 首先,我们要理解多对多关联的基本概念。在数据库设计中,多对多关系意味着一个表中的记录可以与另一个表中的多个记录相关联,反之亦然。...
“Hibernate基于外键的一对多单向关联”这个标题指的是在Java持久化框架Hibernate中,如何通过外键实现一个实体类(如订单)与另一个实体类(如商品)之间的一对多关系,并且这种关联是单向的,即从订单端可以访问到...
本篇文章将深入探讨Hibernate中的一对一外键单向关联。 首先,一对一外键关联指的是一个实体通过外键直接引用另一个实体的主键。在单向关联中,只有其中一个实体知道另一个实体的存在,而另一个实体则无感知。这种...
本教程聚焦于Hibernate中的单向一对多关联映射,这是一个常见的实体关系模型,广泛应用于各种业务场景。 在数据库设计中,一对多关系意味着一个父表记录可以与多个子表记录相关联。例如,一个学生可以有多个课程,...
**标题:“Hibernate的单向多对一”** 在Java编程中,Hibernate是一个强大的对象关系映射(ORM)框架,它简化了数据库与Java对象之间的交互。在数据模型设计中,多对一是常见的关联关系,其中“多”的一方关联“一...
多对多单向关联 <br>需要注意映射规则: <set name="users" table="t_user_role"><br> <key column="roleid"/><br> <many-to-many class="com.bjsxt.hibernate.User" column="userid"/> </set>
"Hibernate单向关联代码示例"是一个实用的学习资源,帮助开发者理解如何在实际项目中实现单向关联。在这个示例中,你将看到两个实体类之间的单向关联,并学习到如何在Java代码中配置和操作这些关联。 单向关联分为...
总的来说,Hibernate的一对一唯一外键关联(单向关联)是通过在一方实体中定义另一方实体的引用,并通过注解或XML映射文件进行配置来实现的。这种关联方式简化了对象关系的管理,但同时也需要开发者在操作关联对象时...
单向多对多关系中,一个实体可以与多个其他实体关联,而其他实体不知道这些关联。例如,一个用户可以订阅多个新闻类别,但类别并不知道哪些用户订阅了它们。配置时,使用`@ManyToMany`注解,并在拥有引用的一方定义`...