- 浏览: 160459 次
- 性别:
- 来自: 杭州
文章分类
最新评论
准备条件:
1. 创建hibernate3工程
2. 导入hibernate的jar包
3. 创建数据库hibernate3,及表customers,orders
一. 一对多双向关联
1. hibernate.cfg.xml
2. Customer.java
3. Order.java
4. Customer.hbm.xml
5. Order.hbm.xml
6. Test.java
二. 一对多双向自身关联
1. hibernate.cfg.xml
2. Category.java
3. Category.hbm.xml
4. Test.java
三. 一对一关联
第一种方式:
1. Hibernate.cfg.xml
2. Student.java
3. Certificate.java
4. Student.hbm.xml
5. Certificate.hbm.xml
6. BM.java
7. StudentDAO.java
8. HibernateUtil.java
第二种方式:
与第一种方式基本相同,唯一不同的是以下文件:
1. Certificate.hbm.xml
四. 多对多关联
1. Hibernate.cfg.xml
2. Student.java
3. Course.java
4. Student.hbm.xml
5. Course.hbm.xml
6. BM.java
7. StudentDAO.java
8. HibernateUtil.java
1. 创建hibernate3工程
2. 导入hibernate的jar包
3. 创建数据库hibernate3,及表customers,orders
一. 一对多双向关联
1. 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="format_sql">true</property> --> <property name="show_sql">true</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate3</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="customer.hbm.xml" /> <mapping resource="order.hbm.xml" /> </session-factory> </hibernate-configuration>
2. Customer.java
package com.hibernate3.bean; import java.util.Set; public class Customer { private Long id; private String name; private Set orders; public Customer(){ } public Customer(String name,Set orders){ this.name=name; this.orders=orders; } 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 getOrders() { return orders; } public void setOrders(Set orders) { this.orders = orders; } }
3. Order.java
package com.hibernate3.bean; public class Order { private Long id; private String orderNumber; private Customer customer; public Order(){ } public Order(String orderNumber,Customer customer){ this.orderNumber=orderNumber; this.customer=customer; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getOrderNumber() { return orderNumber; } public void setOrderNumber(String orderNumber) { this.orderNumber = orderNumber; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
4. Customer.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" > <hibernate-mapping > <class name="com.hibernate3.bean.Customer" table="customers"> <id name="id" column="id" type="long"> <generator class="increment"></generator> </id> <property name="name" column="name" type="string"></property> <set name="orders" cascade="all" inverse="true" fetch="join" > <key column="customer_id"></key> <one-to-many class="com.hibernate3.bean.Order"/> </set> </class> </hibernate-mapping>
5. Order.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" > <hibernate-mapping> <class name="com.hibernate3.bean.Order" table="orders"> <id name="id" column="id" type="long"> <generator class="increment"></generator> </id> <property name="orderNumber" type="string" > <column name="order_number" length="15"></column> </property> <many-to-one name="customer" column="customer_id" class="com.hibernate3.bean.Customer"></many-to-one> </class> </hibernate-mapping>
6. Test.java
package com.hibernate3.bean; import java.util.HashSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { private static SessionFactory sessionFactory; static{ try{ sessionFactory=new Configuration().configure().buildSessionFactory(); }catch (Exception e) { System.out.println("获得sessionFactory出错"); e.printStackTrace(); } } public static void saveCustomerAndOrderWithCascade(){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); Customer customer=new Customer("zs",new HashSet()); Order o1=new Order("no1",customer); Order o2=new Order("no2",customer); customer.getOrders().add(o1); customer.getOrders().add(o2); session.save(customer); tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); ex.printStackTrace(); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } public static void main(String[] args) { saveCustomerAndOrderWithCascade(); } }
二. 一对多双向自身关联
1. 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="format_sql">true</property> --> <property name="show_sql">true</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate3</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="Category.hbm.xml"/> </session-factory> </hibernate-configuration>
2. Category.java
package com.hibernate3.bean; import java.util.Set; public class Category { private Long id; private String name; private Category parentCategory; private Set childCategorys; public Category(){ } public Category(String name,Category parentCategory,Set childCategorys){ this.name=name; this.parentCategory=parentCategory; this.childCategorys=childCategorys; } 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 Category getParentCategory() { return parentCategory; } public void setParentCategory(Category parentCategory) { this.parentCategory = parentCategory; } public Set getChildCategorys() { return childCategorys; } public void setChildCategorys(Set childCategorys) { this.childCategorys = childCategorys; } }
3. Category.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" > <hibernate-mapping > <class name="com.hibernate3.bean.Category" table="categories"> <id name="id" column="id" type="long"> <generator class="increment"></generator> </id> <property name="name" column="name" type="string"></property> <set name="childCategorys" cascade="all" inverse="true" > <key column="category_id"></key> <one-to-many class="com.hibernate3.bean.Category"/> </set> <many-to-one name="parentCategory" column="category_id" class="com.hibernate3.bean.Category"></many-to-one> </class> </hibernate-mapping>
4. Test.java
fpackage com.hibernate3.bean; import java.util.HashSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { private static SessionFactory sessionFactory; static{ try{ sessionFactory=new Configuration().configure().buildSessionFactory(); }catch (Exception e) { System.out.println("获得sessionFactory出错"); e.printStackTrace(); } } public static void saveCategoryWithCascade(){ Session session=sessionFactory.openSession(); Transaction tx=null; try{ tx=session.beginTransaction(); //食口类别 Category footCategory=new Category("foot",null,new HashSet()); //水果类别 Category fruitCategory=new Category("fruit",null,new HashSet()); //蔬菜类别 Category vegetableCategory=new Category("vegetable",null,new HashSet()); //苹果类别 Category appleCategory=new Category("apple",null,new HashSet()); //桔子类别 Category orangeCategory=new Category("orange",null,new HashSet()); //西红柿类别 Category tomatoCategory=new Category("tomato",null,new HashSet()); footCategory.getChildCategorys().add(fruitCategory); fruitCategory.setParentCategory(footCategory); footCategory.getChildCategorys().add(vegetableCategory); vegetableCategory.setParentCategory(footCategory); fruitCategory.getChildCategorys().add(appleCategory); appleCategory.setParentCategory(fruitCategory); fruitCategory.getChildCategorys().add(orangeCategory); orangeCategory.setParentCategory(fruitCategory); vegetableCategory.getChildCategorys().add(tomatoCategory); tomatoCategory.setParentCategory(vegetableCategory); session.save(footCategory); tx.commit(); }catch(Exception ex){ System.out.println("保存数据时出错"); ex.printStackTrace(); if(tx!=null){ tx.rollback(); } }finally{ session.close(); } } public static void main(String[] args) { saveCategoryWithCascade(); } }
三. 一对一关联
第一种方式:
1. 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="format_sql">true</property> --> <property name="show_sql">true</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate3</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="Certificate.hbm.xml" /> <mapping resource="Student.hbm.xml" /> </session-factory> </hibernate-configuration>
2. Student.java
package com.hibernate3.bean; public class Student { private String id; //标识ID private String cardId; //学号 private String name; //姓名 private int age; //年龄 private Certificate cer; //身份证 public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Certificate getCer() { return cer; } public void setCer(Certificate cer) { this.cer = cer; } }
3. Certificate.java
package com.hibernate3.bean; public class Certificate { private String id; private String describe; private Student stu; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } public Student getStu() { return stu; } public void setStu(Student stu) { this.stu = stu; } }
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" > <hibernate-mapping> <class name="com.hibernate3.bean.Student" table="students"> <id name="id" column="id" type="string" unsaved-value="null"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <property name="cardId" column="card_id" type="string"></property> <property name="age" column="age" type="integer"></property> <one-to-one name="cer" class="com.hibernate3.bean.Certificate" cascade="all" ></one-to-one> </class> </hibernate-mapping>
5. Certificate.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" > <hibernate-mapping> <class name="com.hibernate3.bean.Certificate" table="certificates"> <id name="id" column="id" type="string"> <generator class="foreign"> <param name="property">stu</param> </generator> </id> <property name="describe" column="`describe`" type="string"></property> <one-to-one name="stu" class="com.hibernate3.bean.Student" constrained="true" cascade="none"></one-to-one> </class> </hibernate-mapping>
6. BM.java
package com.hibernate3.bean; public class BM { public static void main(String[] args) { Student stu=new Student(); stu.setName("zs"); stu.setCardId("123456"); stu.setAge(33); Certificate cer=new Certificate(); cer.setDescribe("park"); stu.setCer(cer); cer.setStu(stu); StudentDAO.saveObj(stu); } }
7. StudentDAO.java
package com.hibernate3.bean; import org.hibernate.Session; import org.hibernate.Transaction; public class StudentDAO { static Session session=null; static Transaction tx=null; public static void saveObj(Object o){ try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); session.save(o); tx.commit(); }catch(Exception ex){ if(tx!=null){ tx.rollback(); } ex.printStackTrace(); }finally{ HibernateUtil.closeSession(session); } } }
8. HibernateUtil.java
package com.hibernate3.bean; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static{ try{ sessionFactory=new Configuration().configure().buildSessionFactory(); }catch(Exception ex){ System.out.println("获得sessionFactory时出错"); ex.printStackTrace(); } } public static void closeSession(Session session){ if(session!=null){ session.close(); } } public static Session getSession(){ Session session=sessionFactory.openSession(); return session; } }
第二种方式:
与第一种方式基本相同,唯一不同的是以下文件:
1. Certificate.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" > <hibernate-mapping> <class name="com.hibernate2.bean.Certificate" table="certificates"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="describe" column="`describe`" type="string"></property> <many-to-one name="stu" column="stu_id" class="com.hibernate2.bean.Student" unique="true"></many-to-one> </class> </hibernate-mapping>
四. 多对多关联
1. 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="format_sql">true</property> --> <property name="show_sql">true</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate3</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <mapping resource="Student.hbm.xml" /> <mapping resource="Course.hbm.xml" /> </session-factory> </hibernate-configuration>
2. Student.java
package com.hibernate3.bean; import java.util.Set; public class Student { private String id; private String cardId; private String name; private int age; private Set courses; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getCardId() { return cardId; } public void setCardId(String cardId) { this.cardId = cardId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Set getCourses() { return courses; } public void setCourses(Set courses) { this.courses = courses; } }
3. Course.java
package com.hibernate3.bean; import java.util.Set; public class Course { private String id; private String name; private Set students; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Set getStudents() { return students; } public void setStudents(Set students) { this.students = students; } }
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" > <hibernate-mapping > <class name="com.hibernate3.bean.Student" table="students"> <id name="id" column="id" type="string" unsaved-value="null"> <generator class="uuid"></generator> </id> <property name="cardId" column="card_id" type="string"></property> <property name="name" column="name" type="string"></property> <property name="age" column="age" type="integer"></property> <set name="courses" cascade="none" inverse="true" table="student_course" > <key column="stu_id"></key> <many-to-many class="com.hibernate3.bean.Course" column="course_id"></many-to-many> </set> </class> </hibernate-mapping>
5. 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" > <hibernate-mapping > <class name="com.hibernate3.bean.Course" table="course"> <id name="id" column="id" type="string" unsaved-value="null"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <set name="students" cascade="save-update" table="student_course" > <key column="course_id"></key> <many-to-many class="com.hibernate3.bean.Student" column="stu_id"></many-to-many> </set> </class> </hibernate-mapping>
6. BM.java
package com.hibernate3.bean; public class BM { /** * @param args */ public static void main(String[] args) { StudentDAO.mdfChoice(); } }
7. StudentDAO.java
package com.hibernate3.bean; import org.hibernate.Session; import org.hibernate.Transaction; public class StudentDAO { public static void mdfChoice(){ Session session=null; Transaction tx=null; try{ session=HibernateUtil.getSession(); tx=session.beginTransaction(); Student stu=(Student)session.createQuery( "from Student as s where s.name='zs'").uniqueResult(); Course cer=(Course)session.createQuery( "from Course as c where c.name='english'").uniqueResult(); stu.getCourses().add(cer); cer.getStudents().add(stu); tx.commit(); }catch(Exception ex){ if(tx!=null){ tx.rollback(); } ex.printStackTrace(); }finally{ HibernateUtil.closeSession(session); } } }
8. HibernateUtil.java
package com.hibernate3.bean; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static SessionFactory sessionFactory; static{ try{ sessionFactory=new Configuration().configure().buildSessionFactory(); }catch(Exception ex){ System.out.println("获得sessionFactory时出错"); ex.printStackTrace(); } } public static void closeSession(Session session){ if(session!=null){ session.close(); } } public static Session getSession(){ Session session=sessionFactory.openSession(); return session; } }
发表评论
-
struts2.1改变配置文件默认位置
2011-03-31 14:01 798<filter> <init-par ... -
整合SSH2时需注意几点
2010-12-10 16:35 8561.需要导入struts2-spring-plugin-2.1 ... -
ssh2
2010-11-11 13:56 1866准备条件: 1. 建WEB工程:ssh2 2. 建数据库ssh ... -
spring整合hibernate
2010-11-08 08:59 853准备条件: 1. 创建数据库hibernatespring,表 ... -
代理模式
2010-11-08 08:58 857一. 静态代理 1. Subject.java packa ... -
单例模式
2010-11-08 08:57 6691. Singleton.java package com ... -
spring整合struts2
2010-11-08 08:56 742前言: 1. 创建工程strutsspring 2. 导入st ... -
反射案例
2010-11-08 08:56 713案例一: 1. Test.java package com ... -
spring配置文件中集合类型的配置
2010-11-08 08:54 960前言: Spring中对于有些bean会有集合类型的属性,以下 ... -
spring用到的设计模式-工厂模式
2010-11-08 08:53 1262Spring用到的设计模式之一 核心:用到工厂设计模式 1. ... -
读取applicationContext.xml的两种方式
2010-11-08 08:52 962第一种: ClassPathResource cpr=ne ... -
用图形查看spring配置文件依赖关系
2010-11-08 08:51 957myeclipse提供了这样的功能: window->s ... -
spring的基本操作IOC
2010-11-08 08:51 744前言: Spring通过IOC/DI来实现获取对象 准备: 1 ... -
spring的基本操作-反射
2010-11-08 08:50 1114前言: Spring通过反射来实现获取对象 准备: 1. 建S ... -
Struts2整合Hibernate
2010-10-29 19:00 20271. 准备条件: 1.1 在MYSQL中创建hibernate ... -
hibernate基本操作
2010-10-29 18:58 7581.准备条件: 1.1 创建hibernate2工程 1.2 ... -
JDBC
2010-10-29 18:56 776一. JDBC连接Access实例 1. 准备条件 1.1 创 ... -
文件上传
2010-10-26 14:49 6951. upload.jsp <%@ page lan ... -
调用指定Action的方法
2010-10-26 14:48 768主要在配置文件中配置下: 注:如应用于增删改查的类 1. st ... -
I18N
2010-10-26 14:47 819一, 查看当前操作系统中可用的语言 package com ...
相关推荐
在这个" Hibernate级联操作一对多demo "中,我们将探讨如何在一对多的关系中应用级联操作。 一对多关系是数据库中常见的关系类型,它表示一个实体(如部门)可以有多个相关实体(如员工)。在Hibernate中,这种关系...
在Hibernate中,级联操作(Cascading Operations)是一种高级特性,它使得对象之间的关联关系在数据库层面得以同步更新。当我们对一个实体对象进行操作时,可以设置级联属性,让这些操作自动传播到与之相关联的其他...
在 Hibernate 中,级联操作(Cascade)和反向属性(Inverse)是管理对象关系的重要概念,特别是在处理一对多(One-to-Many)或多对一(Many-to-One)关系时。 **级联操作(Cascade)** 级联操作定义了当主对象被...
Hibernate提供了级联操作来简化数据的保存、更新和删除等操作,使得开发者无需手动编写复杂的SQL语句,就能实现关联对象的联动处理。 **描述分析:** 虽然描述部分为空,但根据给定的博文链接,我们可以推测文章...
本主题将深入探讨"hibernate级联增删改查",特别是针对"t_card主表"和"t_person从表"的关系。 首先,级联操作在Hibernate中是一种配置,用于指定当主表中的实体发生变化时,是否以及如何更新或删除关联的从表实体。...
一、Hibernate级联(Cascade) 级联操作允许我们将对一个实体的更改自动应用到与之关联的其他实体。在一对多关系中,如果一个实体(父实体)与多个实体(子实体)关联,设置级联属性可以确保在处理父实体时,子实体...
在Hibernate中,级联操作可以通过`cascade`属性进行配置。主要有以下几种级联类型: 1. `save-update`:当保存或更新父实体时,会自动保存或更新所有关联的子实体。 2. `merge`:在调用`session.merge()`时,不仅...
在探讨Hibernate框架中的级联操作(cascade)与控制权反转(inverse)之前,我们需要先对Hibernate有一个基本的理解。Hibernate是一个开放源代码的对象关系映射(ORM)框架,它为Java应用提供了一种将对象模型映射到...
在文档中提到的场景下,我们来详细探讨Hibernate级联删除的原理和配置方法。 首先,我们要理解关系映射中的几个关键概念。在给出的例子中,有四个表:A、AA、B和BB,它们之间存在一对多(one-to-many)和一对一...
例如,如果在`User`和`Order`的关系中,我们希望由`User`来控制级联操作,可以将`@OneToMany`的`mappedBy`属性设在`User`上,然后设置`inverse`属性为`true`,使得`User`成为关系的主控方,这样在处理`User`时,级联...
在IT领域,尤其是在Java开发中,Hibernate是一个非常重要的对象关系映射(ORM)框架,它允许开发者用面向对象的方式操作数据库。尽管随着Spring Data JPA等现代技术的崛起,Hibernate的使用频率有所下降,但其核心...
### Hibernate基础之关联映射与级联操作 #### 第一部分:主键生成策略与对象状态管理 ##### 主键生成策略 在使用Hibernate框架时,合理的主键生成策略能够极大地简化开发工作并提升应用性能。Hibernate提供了多种...
总的来说,这个项目展示了如何使用Hibernate、Ajax和DWR来实现一个高效、用户友好的省市县三级级联选择功能。开发过程中涉及了ORM、MVC架构、数据库操作以及前端交互等多个方面,是学习和实践Web开发技能的好实例。
1. Hibernate 级联操作与 `cascade` 属性: Hibernate 提供了级联操作功能,使得对一个实体的保存、更新、删除等操作可以自动影响与其关联的其他实体。在 Hibernate 中,`cascade` 属性用于指定这种级联行为。题目...
###Hibernate级联操作 级联操作包括级联添加、修改、删除和查看。在一对多或多对多关系中,配置级联类型可以控制当操作一个对象时,其关联对象的反应。例如,在一对多关系中,配置级联添加后,添加父对象时会自动为...
### Hibernate级联(Cascade)详解 #### 一、概述 Hibernate 是一款强大的对象关系映射 (ORM) 框架,它允许开发人员将 Java 对象模型映射到数据库表,从而极大地简化了数据访问层的开发工作。在 Hibernate 中,...
在级联操作中,Hibernate可能被用来处理省市县和年月日的数据存取,通过HQL(Hibernate Query Language)或者Criteria API来执行SQL查询,获取级联选择的数据。 级联操作的核心在于数据间的关联,例如选择一个省后...
文中提到,直接使用fromObject(list)方法在Hibernate级联操作关联的对象上会报错,如果需要保留级联操作,就必须去掉映射文件中的级联配置。这是因为在进行JSON序列化时,Hibernate的关联关系可能会导致异常。 4. ...
在Hibernate中,可以通过`@OneToMany`注解来定义这种关系,并通过`cascade`属性设置级联操作,例如`CascadeType.ALL`表示所有操作都将级联。 2. **一对一(OneToOne)**:这种关系意味着两个实体之间存在一对一的...