- 浏览: 189803 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (321)
- eclipse (4)
- idea (2)
- Html (8)
- Css (14)
- Javascript (8)
- Jquery (6)
- Ajax Json (4)
- Bootstrap (0)
- EasyUI (0)
- Layui (0)
- 数据结构 (0)
- Java (46)
- DesPattern (24)
- Algorithm (2)
- Jdbc (8)
- Jsp servlet (13)
- Struts2 (17)
- Hibernate (11)
- Spring (5)
- S2SH (1)
- SpringMVC (4)
- SpringBoot (11)
- WebService CXF (4)
- Poi (2)
- JFreeChart (0)
- Shiro (6)
- Lucene (5)
- ElasticSearch (0)
- JMS ActiveMQ (3)
- HttpClient (5)
- Activiti (0)
- SpringCloud (11)
- Dubbo (6)
- Docker (0)
- MySQL (27)
- Oracle (18)
- Redis (5)
- Mybatis (11)
- SSM (1)
- CentOS (10)
- Ant (2)
- Maven (4)
- Log4j (7)
- XML (5)
最新评论
1. Junit4方法详解
2. 一对多映射实现(单向)
3. 级联保存更新
3.1) 不使用级联保存
3.2) 使用级联保存
4) 一对多映射实现(双向)
5) inverse属性
5.1) 手动实现
5.2) 自动实现
6) 级联删除
7) 一对多双向自身关联关系映射
setUpBeforeClass() 类初始化前调用; tearDownAfterClass() 类初始化后调用; setUp() 在测试方法前调用; tearDown() 在测试方法后调用;
JunitTest.java package com.andrew.service; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; public class JunitTest { @BeforeClass public static void setUpBeforeClass() throws Exception { System.out.println("类初始化前调用..."); } @AfterClass public static void tearDownAfterClass() throws Exception { System.out.println("类初始化后调用..."); } @Before public void setUp() throws Exception { System.out.println("在测试方法前调用..."); } @After public void tearDown() throws Exception { System.out.println("在测试方法后调用..."); } @Test public void test() { System.out.println("测试方法"); } } 运行结果: 类初始化前调用... 在测试方法前调用... 测试方法 在测试方法后调用... 类初始化后调用...
2. 一对多映射实现(单向)
hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL --> <property name="show_sql">true</property> <!-- 自动更新表结构 --> <property name="hbm2ddl.auto">update</property> <!-- Mapper --> <mapping resource="com/andrew/model/Student.hbm.xml"/> <mapping resource="com/andrew/model/Class.hbm.xml"/> </session-factory> </hibernate-configuration> HibernateUtil.java package com.andrew.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { // 1. 实例化配置文件 Configuration configuration = new Configuration().configure(); // 2. 实例化服务登记 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // 3. 获取Session工厂 return configuration.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sessionFactory; } } Class.java package com.andrew.model; public class Class { 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; } } Class.hbm.xml <?xml version="1.0"?> <!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.andrew.model"> <class name="Class" table="t_class"> <id column="classId" name="id"> <generator class="native"/> </id> <property name="name" column="className"/> </class> </hibernate-mapping> Student.java package com.andrew.model; public class Student { private long id; private String name; private Class c; 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 Class getC() { return c; } public void setC(Class c) { this.c = c; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", c=" + c + "]"; } } Student.hbm.xml <?xml version="1.0"?> <!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.andrew.model"> <class name="Student" table="t_student"> <id column="stuId" name="id"> <generator class="native"/> </id> <property name="name" column="stuName"/> <many-to-one name="c" column="classId" class="com.andrew.model.Class"></many-to-one> </class> </hibernate-mapping> StudentTest.java package com.andrew.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentTest { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); Session session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 Class c = new Class(); c.setName("10级1班"); session.save(c); Student s1 = new Student(); s1.setName("张三"); s1.setC(c); session.save(s1); Student s2 = new Student(); s2.setName("李四"); s2.setC(c); session.save(s2); session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } }
3. 级联保存更新
在<many-to-one>这端,cascade默认是”none”,假如我们希望在持久化多的一端的时候,自动级联保存和更新一的一端,我们可以把cascade设置成”save-update”; 内存空间 临时对象 持久化对象(在session缓存中) class对象 student1对象 student2对象
3.1) 不使用级联保存
Student.hbm.xml <many-to-one name="c" column="classId" class="com.andrew.model.Class"></many-to-one> StudentJunitTest.java package com.andrew.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentJunitTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testSaveClassAndStudent() { Class c = new Class(); c.setName("10级1班"); session.save(c); Student s1 = new Student(); s1.setName("张三"); s1.setC(c); Student s2 = new Student(); s2.setName("李四"); s2.setC(c); session.save(s1); session.save(s2); } } 运行结果: Hibernate: insert into t_class (className) values (?) Hibernate: insert into t_student (stuName, classId) values (?, ?) Hibernate: insert into t_student (stuName, classId) values (?, ?)
3.2) 使用级联保存
Student.hbm.xml <many-to-one name="c" column="classId" class="com.andrew.model.Class" cascade="save-update"></many-to-one> StudentJunitTest.java package com.andrew.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentJunitTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testSaveClassAndStudentWithCascade() { Class c = new Class(); c.setName("10级1班"); Student s1 = new Student(); s1.setName("张三"); s1.setC(c); Student s2 = new Student(); s2.setName("李四"); s2.setC(c); session.save(s1); session.save(s2); } } 运行结果: Hibernate: insert into t_class (className) values (?) Hibernate: insert into t_student (stuName, classId) values (?, ?) Hibernate: insert into t_student (stuName, classId) values (?, ?)
4) 一对多映射实现(双向)
新建项目Hibernate04-2
hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL --> <property name="show_sql">true</property> <!-- 自动更新表结构 --> <property name="hbm2ddl.auto">update</property> <!-- Mapper --> <mapping resource="com/andrew/model/Student.hbm.xml"/> <mapping resource="com/andrew/model/Class.hbm.xml"/> </session-factory> </hibernate-configuration> HibernateUtil.java package com.andrew.util; import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { // 1. 实例化配置文件 Configuration configuration = new Configuration().configure(); // 2. 实例化服务登记 ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); // 3. 获取Session工厂 return configuration.buildSessionFactory(serviceRegistry); } public static SessionFactory getSessionFactory() { return sessionFactory; } } Student.java package com.andrew.model; public class Student { private long id; private String name; private Class c; 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 Class getC() { return c; } public void setC(Class c) { this.c = c; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", c=" + c.getName() + "]"; } } Student.hbm.xml <?xml version="1.0"?> <!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.andrew.model"> <class name="Student" table="t_student"> <id column="stuId" name="id"> <generator class="native"/> </id> <property name="name" column="stuName"/> <many-to-one name="c" column="classId" class="com.andrew.model.Class" cascade="save-update"></many-to-one> </class> </hibernate-mapping> Class.java package com.andrew.model; import java.util.HashSet; import java.util.Set; public class Class { private long id; private String name; private Set<Student> students = new HashSet<Student>(); 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 Set<Student> getStudents() { return students; } public void setStudents(Set<Student> students) { this.students = students; } } Class.hbm.xml <?xml version="1.0"?> <!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.andrew.model"> <class name="Class" table="t_class"> <id column="classId" name="id"> <generator class="native"/> </id> <property name="name" column="className"/> <set name="students" cascade="save-update"> <key column="classId"></key> <one-to-many class="com.andrew.model.Student"/> </set> </class> </hibernate-mapping> StudentTest.java package com.andrew.service; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testSaveClassAndStudent() { Class c = new Class(); c.setName("10级1班"); Student s1 = new Student(); s1.setName("张三"); Student s2 = new Student(); s2.setName("李四"); c.getStudents().add(s1); c.getStudents().add(s2); session.save(c); } @Test public void getStudentsByClass() { Class c = (Class)session.get(Class.class, Long.valueOf(1)); Set<Student> students = c.getStudents(); Iterator it = students.iterator(); while (it.hasNext()) { Student s = (Student)it.next(); System.out.println(s); } } } 运行结果: Hibernate: insert into t_class (className) values (?) Hibernate: insert into t_student (stuName, classId) values (?, ?) Hibernate: insert into t_student (stuName, classId) values (?, ?) Hibernate: update t_student set classId=? where stuId=? Hibernate: update t_student set classId=? where stuId=? Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=? Student [id=1, name=张三, c=10级1班] Student [id=2, name=李四, c=10级1班]
5) inverse属性
在多的一端维护一的一端
5.1) 手动实现
StudentTest.java package com.andrew.service; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testAdd() { Class c = new Class(); c.setName("10级2班"); Student s1=new Student(); s1.setName("王五"); session.save(c); session.save(s1); } @Test public void testInverse() { Class c = (Class)session.get(Class.class, Long.valueOf(1)); Student s = (Student)session.get(Student.class, Long.valueOf(1)); s.setC(c); c.getStudents().add(s); } } testAdd运行结果: Hibernate: insert into t_class (className) values (?) Hibernate: insert into t_student (stuName, classId) values (?, ?) 1 10级2班 1 王五 NULL testInverse运行结果: Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? Hibernate: select student0_.stuId as stuId1_1_0_, student0_.stuName as stuName2_1_0_, student0_.classId as classId3_1_0_ from t_student student0_ where student0_.stuId=? Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=? Hibernate: update t_student set stuName=?, classId=? where stuId=? Hibernate: update t_student set classId=? where stuId=? 1 10级2班 1 王五 1
5.2) 自动实现
Class.hbm.xml ... <set name="students" cascade="save-update" inverse="true"> <key column="classId"></key> <one-to-many class="com.andrew.model.Student"/> </set> ... testAdd运行结果: Hibernate: insert into t_class (className) values (?) Hibernate: insert into t_student (stuName, classId) values (?, ?) 1 10级2班 1 王五 NULL testInverse运行结果: Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? Hibernate: select student0_.stuId as stuId1_1_0_, student0_.stuName as stuName2_1_0_, student0_.classId as classId3_1_0_ from t_student student0_ where student0_.stuId=? Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=? Hibernate: update t_student set stuName=?, classId=? where stuId=? 1 10级2班 1 王五 1
6) 级联删除
cascade="delete",危险操作,删除关联表的信息 Class.hbm.xml <set name="students" cascade="delete" inverse="true"> <key column="classId"></key> <one-to-many class="com.andrew.model.Student"/> </set> StudentTest.java package com.andrew.service; import java.util.Iterator; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Class; import com.andrew.model.Student; import com.andrew.util.HibernateUtil; public class StudentTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testDeleteClassCascade() { Class c = (Class)session.get(Class.class, Long.valueOf(1)); session.delete(c); } } 运行结果: Hibernate: select class0_.classId as classId1_0_0_, class0_.className as classNam2_0_0_ from t_class class0_ where class0_.classId=? Hibernate: select students0_.classId as classId3_0_0_, students0_.stuId as stuId1_1_0_, students0_.stuId as stuId1_1_1_, students0_.stuName as stuName2_1_1_, students0_.classId as classId3_1_1_ from t_student students0_ where students0_.classId=? Hibernate: delete from t_student where stuId=? Hibernate: delete from t_class where classId=?
7) 一对多双向自身关联关系映射
hibernate.cfg.xml <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 数据库连接设置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate4</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制台显示SQL --> <property name="show_sql">true</property> <!-- 自动更新表结构 --> <property name="hbm2ddl.auto">update</property> <!-- Mapper --> <mapping resource="com/andrew/model/Student.hbm.xml"/> <mapping resource="com/andrew/model/Class.hbm.xml"/> <mapping resource="com/andrew/model/Node.hbm.xml"/> </session-factory> </hibernate-configuration> Node.java package com.andrew.model; import java.util.HashSet; import java.util.Set; public class Node { private long id; private String name; private Node parentNode; private Set<Node> childNodes = new HashSet<Node>(); 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 Node getParentNode() { return parentNode; } public void setParentNode(Node parentNode) { this.parentNode = parentNode; } public Set<Node> getChildNodes() { return childNodes; } public void setChildNodes(Set<Node> childNodes) { this.childNodes = childNodes; } } Node.hbm.xml <?xml version="1.0"?> <!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.andrew.model"> <class name="Node" table="t_node"> <id name="id" column="nodeId"> <generator class="native"></generator> </id> <property name="name" column="nodeName"></property> <many-to-one name="parentNode" column="parentId" class="com.andrew.model.Node" cascade="save-update"></many-to-one> <set name="childNodes" inverse="true"> <key column="parentId"></key> <one-to-many class="com.andrew.model.Node"/> </set> </class> </hibernate-mapping> NodeTest.java package com.andrew.service; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.junit.After; import org.junit.Before; import org.junit.Test; import com.andrew.model.Node; import com.andrew.util.HibernateUtil; public class NodeTest { private SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); private Session session; @Before public void setUp() throws Exception { session = sessionFactory.openSession(); // 1. 生成一个session session.beginTransaction(); // 2. 开启事务 } @After public void tearDown() throws Exception { session.getTransaction().commit(); // 3. 提交事务 session.close(); // 4. 关闭session } @Test public void testSaveMenu() { Node node = new Node(); node.setName("根节点"); Node subNode1 = new Node(); subNode1.setName("子节点1"); Node subNode2 = new Node(); subNode2.setName("子节点2"); subNode1.setParentNode(node); subNode2.setParentNode(node); session.save(subNode1); session.save(subNode2); } } 运行结果: Hibernate: insert into t_node (nodeName, parentId) values (?, ?) Hibernate: insert into t_node (nodeName, parentId) values (?, ?) Hibernate: insert into t_node (nodeName, parentId) values (?, ?) 1 根节点 NULL 2 子节点1 1 3 子节点2 1
发表评论
-
Hibernate高级配置
2019-01-16 08:52 3531. 配置数据库连接池 反问数据库,需要不断的创建和释放 ... -
Hibernate查询方式
2019-01-16 08:52 3961) Hibernate查询方式简介 新建项目Hiber ... -
Hibernate检索策略
2019-01-15 14:16 3731. Hibernate检索策略 新建项目Hiberna ... -
Hibernate映射关系
2019-01-15 14:16 4371. Hibernate映射关系 新建项目Hiberna ... -
Hibernate映射继承
2019-01-15 14:16 4591. Hibernate映射继承 新建项目Hiberna ... -
Hibernate映射类型
2019-01-15 08:54 3841. 基本类型映射 Hibernat ... -
Hibernate操作对象
2019-01-15 08:54 3291. Hibernate中四种对象状态 临时状态(tra ... -
映射对象标识符(OID)
2019-01-14 09:01 4491. Hibernate用对象标识符(OID)来区分对象 ... -
Hibernate4的CRUD
2019-01-14 09:01 4161. Hibernate4的CRUD 新建项目Hiber ... -
Hibernate简介
2019-01-14 08:55 3611. Hibernate简介 Hibernate官网:htt ...
相关推荐
SpringBoot 中 MyBatis 表关联映射关系(一对多嵌套:结果方式) 在 SpringBoot 中,MyBatis 是一个非常流行的持久层框架,它提供了强大的持久化功能,能够将 Java 对象与数据库表进行映射。在实际开发中,我们经常...
### Hibernate映射一对多关联关系 #### 知识点概览 - **概念解析**:一对多(One-to-Many)关联关系的概念及其在Hibernate中的实现方式。 - **域模型设计**:创建具有一个实体与多个实体关联的域模型。 - **关系...
在处理复杂的数据库关联关系时,比如一对一、一对多、多对一、多对多等,Mybatis提供了灵活的映射机制。本篇将详细讲解如何在Mybatis中实现一对多关联映射的查询操作。 首先,我们要明确一对多关联映射的基本概念。...
本篇文章将深入探讨Hibernate中的一对多和多对多关系映射。 首先,让我们理解一对多关联映射。在数据库中,一对多关系意味着一个表中的记录可以与另一个表中的多个记录相关联。在Hibernate中,这可以通过在实体类中...
在 Springboot 中,MyBatis 提供了强大的表关联映射关系机制,可以实现一对一、多对一、多对多等各种关联关系。在本文中,我们将详细介绍 Springboot 中 MyBatis 表关联映射关系的实现机制,特别是针对一对一关联...
此外,多对一关联实际上与一对多关联非常类似,唯一的区别在于外键的方向。在实际应用中,这种关联经常用于表示一个教师有多门课程,但每门课程只有一个任教教师。 总结来说,Hibernate的关联关系映射允许开发者...
在本篇关于MyBatis框架的学习中,我们将深入探讨一对一关联映射和一对多关联映射的概念、配置以及在实际开发中的应用。MyBatis是一个优秀的持久层框架,它允许开发者将SQL语句直接写在XML配置文件或者注解中,从而...
在Java的持久化框架Hibernate中,多对多关联映射是一种常见的关系数据库模型映射方式,用于处理两个实体之间存在多个实例互相关联的情况。本文将深入探讨Hibernate如何实现多对多关联映射,并通过实际例子解析相关...
你可以为每个实体创建一对多映射,并在业务逻辑中处理关联关系。 例如,对于用户和角色的关系,可以先配置用户和中间表的映射,再配置角色和中间表的映射: ```xml <!-- 用户映射 --> SELECT r.* FROM roles r...
除了XML映射,Ibatis也支持使用注解进行一对多映射,但配置方式略有不同。总的来说,理解并熟练运用Ibatis的一对多关系映射是优化数据访问效率,提高代码可读性和可维护性的重要技能。 在实际开发中,合理地使用...
本文将深入探讨Hibernate中的一对多、多对一以及多对多映射。 1. 一对多与多对一映射: 在现实世界中,许多关系表现为一对多或多对一的形式。例如,一个老师可以教授多个学生,而一个学生只能被一个老师教导;同样...
包含《多对多双向关联映射》《多对一单向关联映射》《多对一双向关联映射》《一对多单向关联映射》等文档,并有图解及例子,非常适合新手学习,尤其是刚刚接触hibernate,对映射关系不清楚的。。。。
在处理复杂的关联关系时,MyBatis的一对多映射机制显得尤为重要。一对多映射指的是一个父类实体对应多个子类实体的关系,例如一个学生可以有多个课程,一个部门可以有多名员工等。 一、一对多映射的基本概念 在...
Mybatis关联映射是数据库操作中的一个重要概念,它允许我们在SQL查询中处理一对多、多对一、多对多等复杂关系。在这个"Mybatis关联映射Demo"中,我们将深入探讨如何在Mybatis框架中实现这些关系映射,以便更好地理解...
综上所述,Hibernate提供了丰富的关联关系映射机制,包括一对一、一对多和多对多关联,每种关联又可以细分为单向和双向,以及基于主键和基于外键的不同实现方式。正确理解和选择合适的关联关系对于构建高效、可维护...
在Java的持久化框架Hibernate中,一对一(One-to-One)关联映射是一种常见的关系数据库模型映射方式。这种关联通常发生在两个实体之间,且每个实体都只有一个对应的实例。本篇文章将详细探讨双向一对一主键关联映射...
在本篇文章中,我们将深入探讨如何实现双向关联的一对多映射,以及在实际应用中可能会遇到的问题和解决方案。 首先,我们需要了解什么是双向关联。在Hibernate的一对多关联中,双向关联意味着双方都可以通过导航...
在实际的项目开发中,数据库中的表与表之间经常存在各种关联关系,比如一对一(One-to-One)、一对多(One-to-Many)和多对多(Many-to-Many)关系。本篇文章将重点讲解如何在 MyBatis 中实现一对一的关联映射。 一...
在本章中,我们将深入探讨Hibernate中的关联映射,包括一对多、多对一以及多对多的关系。这些映射关系对于理解如何在Java应用程序中有效地管理数据库对象至关重要。 首先,我们来解决描述中提到的问题。`...