- 浏览: 139659 次
文章分类
最新评论
-
wangyudong:
很有帮助的代码实例,测试Spring Boot REST AP ...
1、springboot入门案例helloworld与集成hibernate -
wangyudong:
学习了,的确是一个非常好的Spring boot实例,很快就写 ...
1、springboot入门案例helloworld与集成hibernate
pom.xml
hibernate.cfg.xml
一对一,方案一使用主从关系,从表id生成依赖主表id
一对一 方案二:从表创建一个外键
多对一
一对多
多对多 方案一
多对多 方案二:分成两个一对多
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chen</groupId> <artifactId>hibernate_demo01</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <build/> <dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.18.1-GA</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> </dependencies> </project>
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"> <hibernate-configuration> <session-factory> <property name="show_sql">true</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.pool.size">20</property> <property name="connection.characterEncoding">utf8</property> <property name="current_session_context_class">thread</property> <mapping class="com.chen.pojo.Teacher"/> <mapping class="com.chen.pojo.Course"/> <mapping class="com.chen.pojo.TeacherRelCourse"/> </session-factory> </hibernate-configuration>
一对一,方案一使用主从关系,从表id生成依赖主表id
主表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.PrimaryKeyJoinColumn; import javax.persistence.Table; @Table(name="t_person") @Entity public class Person { private Long id ; private String name ; private Idcard idcard; @Id @GeneratedValue @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToOne @PrimaryKeyJoinColumn public Idcard getIdcard() { return idcard; } public void setIdcard(Idcard idcard) { this.idcard = idcard; } } 从表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToOne; import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; import org.hibernate.annotations.Parameter; @Table(name="t_idcard") @Entity public class Idcard { private Long id ; private String name ; private Person person ; @Id @GenericGenerator(name="pkGenerator",strategy="foreign",parameters={@Parameter(name="property",value="person")}) @GeneratedValue(generator="pkGenerator") @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToOne(targetEntity=Person.class,mappedBy="idcard") public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } } 测试案例 package com.chen.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; import com.chen.pojo.Idcard; import com.chen.pojo.Person; public class One2OneDemo1 { private SessionFactory sessionFactory ; public One2OneDemo1() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Session session = sessionFactory.openSession(); Transaction tr = session.beginTransaction(); Idcard idcard = new Idcard(); idcard.setName("idcard"); Person p = new Person(); p.setName("chen"); p.setIdcard(idcard); idcard.setPerson(p); session.save(p); session.save(idcard); tr.commit(); session.close(); } @Test public void test2(){ Session s = sessionFactory.openSession(); Transaction tr = s.beginTransaction(); Person p = (Person) s.get(Person.class, 4L); System.out.println(p.getName()+" "+p.getId()+" "+p.getIdcard().getName()); tr.commit(); s.close(); } }
一对一 方案二:从表创建一个外键
主表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name="t_husband") @Entity public class Husband { private Long id ; private String name ; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } } 从表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Table(name="t_wife") @Entity public class Wife { private Long id ; private Husband husband ; private String name ; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @OneToOne(fetch=FetchType.LAZY) @JoinColumn(name="h_id") public Husband getHusband() { return husband; } public void setHusband(Husband husband) { this.husband = husband; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } } 测试案例 package com.chen.test; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; import com.chen.pojo.Husband; import com.chen.pojo.Wife; public class One2OneDemo2 { private SessionFactory sessionFactory ; public One2OneDemo2() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Session s = sessionFactory.openSession(); Transaction tr = s.beginTransaction(); Husband h = new Husband(); h.setName("husband"); Wife w = new Wife(); w.setName("wife"); w.setHusband(h); s.save(h); s.save(w); tr.commit(); s.close(); } @Test public void test2(){ Session s = sessionFactory.openSession(); Wife w = (Wife) s.get(Wife.class,1L); System.out.println(w.getName()+" "+w.getHusband().getName()); s.close(); } }
多对一
一 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name="t_temp_department") @Entity public class Department { private Long id ; private String departmentName ; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="department_name") public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } } 多 方表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToOne; import javax.persistence.Table; @Table(name="t_employee") @Entity public class Employee { private Long id ; private String name ; private Department department; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToOne(targetEntity=Department.class) public Department getDepartment() { return department; } public void setDepartment(Department department) { this.department = department; } } 测试案例 package com.chen.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; import com.chen.pojo.Department; import com.chen.pojo.Employee; public class Many2OneDemo1 { private SessionFactory sessionFactory ; public Many2OneDemo1() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Session session = sessionFactory.openSession(); Department department = new Department(); department.setDepartmentName("department_2"); Employee emp = new Employee(); emp.setName("emp_2"); emp.setDepartment(department); Transaction tr = session.beginTransaction(); session.save(emp); session.save(department); tr.commit(); session.close(); } @Test public void test2(){ Session session = sessionFactory.openSession(); String hql = "from Employee t where t.id=1"; Query query = session.createQuery(hql); List<Employee> list =(List<Employee>)query.list(); for(Employee e : list){ System.out.println(e.getName()+" "+e.getDepartment().getDepartmentName()); } session.close(); } }
一对多
一 package com.chen.pojo; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToMany; import javax.persistence.Table; @Table(name="t_temp_department") @Entity public class Department2 { private Long id ; private String departmentName ; private Set<Employee2> emps; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="department_name") public String getDepartmentName() { return departmentName; } public void setDepartmentName(String departmentName) { this.departmentName = departmentName; } @OneToMany(targetEntity=Employee2.class) @JoinColumn(name="department_id") public Set<Employee2> getEmps() { return emps; } public void setEmps(Set<Employee2> emps) { this.emps = emps; } } 多 方表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Table(name="t_employee") @Entity public class Employee2 { private Long id ; private String name ; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } } 测试案例 package com.chen.test; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; import com.chen.pojo.Department2; public class One2ManyDemo1 { private SessionFactory sessionFactory ; public One2ManyDemo1() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Session session = sessionFactory.openSession(); String hql = "from Department2 t where t.id=1"; Query query = session.createQuery(hql); List<Department2> list =(List<Department2>)query.list(); for(Department2 d : list){ System.out.println(d.getDepartmentName()+" "+d.getEmps().size()); } session.close(); } }
多对多 方案一
package com.chen.pojo; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.ManyToMany; import javax.persistence.Table; @Table(name="t_admin") @Entity public class Admin { private Long id ; private String name ; private Set<Role> roles ; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany(mappedBy="admins") public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } } package com.chen.pojo; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.Table; @Table(name="t_role") @Entity public class Role { private Long id ; private String name ; private Set<Admin> admins; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @ManyToMany @JoinTable(name="t_role_admin",joinColumns={@JoinColumn(name="r_id")}, inverseJoinColumns={@JoinColumn(name="a_id")}) public Set<Admin> getAdmins() { return admins; } public void setAdmins(Set<Admin> admins) { this.admins = admins; } } 测试案例 package com.chen.test; import java.util.HashSet; import java.util.Set; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.junit.Test; import com.chen.pojo.Admin; import com.chen.pojo.Role; public class Many2ManyDemo1 { private SessionFactory sessionFactory ; public Many2ManyDemo1() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Set<Admin> sets1 = new HashSet<Admin>(); Set<Admin> sets2 = new HashSet<Admin>(); Session s = sessionFactory.openSession(); Transaction tr = s.beginTransaction(); Admin a1 = new Admin(); a1.setName("name1"); sets1.add(a1); sets2.add(a1); Admin a2 = new Admin(); a2.setName("name2"); sets2.add(a2); Role r1 = new Role(); r1.setName("role1"); Role r2 = new Role(); r2.setName("role2"); r1.setAdmins(sets1); r2.setAdmins(sets2); s.save(a1); s.save(a2); s.save(r1); s.save(r2); tr.commit(); s.close(); } }
多对多 方案二:分成两个一对多
一表1 package com.chen.pojo; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Table(name="t_teacher") @Entity public class Teacher { private Long id ; private String name ; private Set<TeacherRelCourse> coursRels; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy="teacher") @LazyCollection(LazyCollectionOption.EXTRA) public Set<TeacherRelCourse> getCoursRels() { return coursRels; } public void setCoursRels(Set<TeacherRelCourse> coursRels) { this.coursRels = coursRels; } } 一表2 package com.chen.pojo; import java.util.Set; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import org.hibernate.annotations.LazyCollection; import org.hibernate.annotations.LazyCollectionOption; @Table(name="t_course") @Entity public class Course { private Long id ; private String name ; private Set<TeacherRelCourse> teacherRels; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } @OneToMany(mappedBy="course") @LazyCollection(LazyCollectionOption.EXTRA) public Set<TeacherRelCourse> getTeacherRels() { return teacherRels; } public void setTeacherRels(Set<TeacherRelCourse> teacherRels) { this.teacherRels = teacherRels; } } 多 方表 package com.chen.pojo; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Table(name="t_teacher_course") @Entity public class TeacherRelCourse { private Long id ; private Teacher teacher; private Course course; @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name="id") public Long getId() { return id; } public void setId(Long id) { this.id = id; } @ManyToOne @JoinColumn(name="t_id") public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } @ManyToOne @JoinColumn(name="c_id") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } } 测试案例 package com.chen.test; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.AnnotationConfiguration; import org.hibernate.classic.Session; import org.junit.Test; import com.chen.pojo.Course; import com.chen.pojo.Teacher; import com.chen.pojo.TeacherRelCourse; public class Many2ManyDemo2 { private SessionFactory sessionFactory ; public Many2ManyDemo2() { if(sessionFactory==null){ sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); } } @Test public void test1(){ Session s = sessionFactory.openSession(); Transaction tr = s.beginTransaction(); TeacherRelCourse tc1 = new TeacherRelCourse(); TeacherRelCourse tc2 = new TeacherRelCourse(); TeacherRelCourse tc3 = new TeacherRelCourse(); Teacher t1 = new Teacher(); t1.setName("teacher1"); tc1.setTeacher(t1); Teacher t2 = new Teacher(); t2.setName("teacher2"); tc2.setTeacher(t2); tc3.setTeacher(t2); Course c1 = new Course(); c1.setName("course1"); tc1.setCourse(c1); tc2.setCourse(c1); Course c2 = new Course(); c2.setName("course2"); tc3.setCourse(c2); s.save(t1); s.save(t2); s.save(c1); s.save(c2); s.save(tc1); s.save(tc2); s.save(tc3); tr.commit(); s.close(); } @Test public void test2(){ Session s = sessionFactory.openSession(); Teacher t1 = (Teacher)s.get(Teacher.class,1L); System.out.println(t1.getName()+" "+t1.getCoursRels().size()); for(TeacherRelCourse obj : t1.getCoursRels()){ System.out.println(obj.getCourse().getName()); } s.close(); } @Test public void test3(){ Session s = sessionFactory.openSession(); Teacher t1 = (Teacher)s.get(Teacher.class,2L); System.out.println(t1.getName()+" "+t1.getCoursRels().size()); for(TeacherRelCourse obj : t1.getCoursRels()){ System.out.println(obj.getCourse().getName()); } s.close(); } }
发表评论
-
spring data jpa简单案例
2016-03-29 10:16 743pom.xml <project xmlns=&qu ... -
mybatis入门示例传智燕青讲解二----高级映射和二级缓存及整合spring及反向工程
2016-03-15 10:00 847二级缓存及高级映射 sqlMapConfig.xml &l ... -
mybatis入门示例传智燕青讲解一
2016-03-11 15:51 2099示例一:增删改查 sqlMap ... -
mybatis入门案例
2016-03-10 14:41 502sqlMapConfiger.xml <?xml v ... -
spring3+mybatis整合案例
2016-02-19 11:21 703pom.xml <project xmlns=&qu ... -
springmvc+hibernate4搭建案例
2016-02-17 09:58 955pom.xml <project xmlns=&qu ... -
hibernate和mybatis比较
2016-01-07 15:13 1251Hibernate简介 Hibernate对数据库结构提供了较 ... -
mybaits简述及原理概述
2016-01-07 11:40 3051Mybatis是一个基于Java的持久层框架。 MyBati ... -
hibernate基本原理
2016-01-04 17:38 3020hibernate是一个基于ORM的持久化框架。它是对jdbc ... -
springmvc与struts2比较
2015-12-30 17:31 839springmvc与struts2比较 1、springmvc ... -
Struts2简述及工作原理
2015-12-30 11:14 10326Struts2是实现MVC(Model-View-Contro ... -
springmvc学习总结01---简述及原理模型
2015-12-28 17:38 14241、用户发起request请求至控制器(Controlle ... -
Spring框架学习总结01----Spring简述
2015-12-28 10:30 1953学习Spring之前,首先要 ... -
spring3.0.7中各个jar包的作用总结
2015-12-28 09:25 932org.springframework.context-3.0 ... -
springmvc非注解方式
2015-11-05 21:36 857springmvc 模型原理 springmvc. ...
相关推荐
本案例主要探讨的是Hibernate中的一对多关系映射,这是一种常见的对象关系映射(ORM)配置,用于表示一个实体与多个其他实体之间的关联。 在传统的数据库设计中,一对多关系是指一个表(父表)中的记录可以与另一个...
这个"spring,springMVC,Hibernate全注解案例"旨在通过注解的方式,帮助开发者理解如何在不编写XML配置的情况下,实现数据库与应用程序的交互。 Spring框架是一个全面的后端开发解决方案,它包含了依赖注入(DI)和...
Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射
本案例主要探讨的是Hibernate中的一对多和多对多关系映射的实现,这对于理解和掌握ORM(Object-Relational Mapping)技术至关重要。 一、一对多关系 在数据库中,一对多关系是指一个表中的记录可以对应另一个表中...
描述中的链接指向了一篇由“fangguanhong”在iteye博客上发表的文章,虽然具体内容没有提供,但我们可以根据标题推测该文章可能详细讲解了一个具体的Hibernate一对多关系的实现步骤和示例代码。 在Java的Hibernate...
在这个“使用Mysql+Maven+Hibernate的一对一,一对多的CURD小案例”中,我们将探讨如何在Java Web开发中结合这三个关键技术实现数据的创建(Create)、读取(Read)、更新(Update)和删除(Delete)操作。...
标题"hibernate一对多,多对一,一对多双向关联"指涉的是Hibernate框架中常见的关系映射概念。在数据库设计中,实体间的关系通常包括一对一(One-to-One)、一对多(One-to-Many)和多对一(Many-to-One)关系,而在...
通过这个项目,学习者可以深入理解Hibernate双向一对多关系的配置、使用及其实现细节,从而提升数据库操作和对象持久化的技能。实践中遇到的问题和解决方法也会帮助巩固理论知识,提升实际开发能力。
在Hibernate中,实现多对多关系映射主要通过以下几种方式: 1. **使用`@ManyToMany`注解**:在Java类的属性上添加`@ManyToMany`注解,指定与之关联的实体类以及关联的表字段。 2. **定义中间表**:如果需要自定义...
在Java的持久化框架...总结,通过这个小案例,我们可以了解到如何在Hibernate中配置和使用一对多关系,以及相关的级联操作、懒加载和性能优化策略。熟练掌握这些技巧将使我们在处理复杂的数据库关联时更加得心应手。
本案例主要探讨的是Hibernate 4.3.8版本中如何使用注解和XML配置实现一对多的双向关联关系。这种关系在实际的业务场景中十分常见,例如一个用户可以有多个订单,一个订单对应一个用户。 首先,我们来看一下注解的...
在数据库中,多对多关系是指两个表之间存在一对多的关系,而这两个一对多关系又互为对方的一对多。例如,学生和课程的关系,一个学生可以选修多个课程,同时一个课程也可以被多个学生选修。在Hibernate中,我们通过...
标题"Hibernate多对多实例+数据库代码"揭示了我们将在讨论一个使用Hibernate框架实现的多对多关系映射的实际案例。在这个实例中,开发人员将两个或多个实体之间的复杂关联转化为简单易懂的数据库操作。描述中提到...
在Post类上使用`@ManyToOne`注解,Category类上不需额外注解,因为默认已经有一对多的关系。 7. **级联操作**:在关系映射中,可以通过`cascade`属性定义一个操作(如保存、删除)是否应级联到关联的对象。例如,...
总的来说,这个“hibernate 多对多 映射实例,学生 选课”项目涵盖了Hibernate的多对多关系映射、数据库设计、对象模型构建、事务管理等多个方面,是一个很好的学习和实践Hibernate的实战案例。通过深入理解和实践这...
4. **关联注解**:在处理一对一、一对多、多对一、多对多关联时,Hibernate提供了相应的注解。例如,`@OneToOne`、`@ManyToOne`、`@OneToMany`和`@ManyToMany`。例如,一个用户可能有多个订单,可以这样表示: ```...
在本文中,我们将深入探讨如何将Spring框架与Hibernate ORM(对象关系映射)框架整合,以便在实际项目中实现高效、灵活的数据访问层。Spring以其强大的依赖注入和管理能力,结合Hibernate的数据库操作功能,可以构建...
《Wicket6.0_Spring3.1_Hibernate4.1_EJB全注解实例》是一个集成开发案例,它结合了四个重要的Java技术框架:Wicket、Spring、Hibernate和EJB,同时使用JTA(Java Transaction API)进行事务管理。这个实例在...
在Java Web开发中,"Struts+Hibernate实现用户登录案例"是一个常见的应用场景,它结合了两个重要的技术框架:Struts和Hibernate。Struts是MVC(Model-View-Controller)设计模式的一种实现,用于处理用户的请求和...
在本篇关于“在 Hibernate 中实现复杂的数据映射”的文章中,我们将深入探讨如何使用 Hibernate 框架处理不同类型的关联关系,包括一对一、一对多和多对多。这些关系在数据库设计中十分常见,而 Hibernate 提供了...