`
lijunaccp
  • 浏览: 160459 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

hibernate级联操作

阅读更多
准备条件:
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;
	}
}
分享到:
评论

相关推荐

    Hibernate级联操作一对多demo

    在这个" Hibernate级联操作一对多demo "中,我们将探讨如何在一对多的关系中应用级联操作。 一对多关系是数据库中常见的关系类型,它表示一个实体(如部门)可以有多个相关实体(如员工)。在Hibernate中,这种关系...

    Hibernate的级联操作(增,删,该)

    在Hibernate中,级联操作(Cascading Operations)是一种高级特性,它使得对象之间的关联关系在数据库层面得以同步更新。当我们对一个实体对象进行操作时,可以设置级联属性,让这些操作自动传播到与之相关联的其他...

    Hibernate级联操作.docx

    在 Hibernate 中,级联操作(Cascade)和反向属性(Inverse)是管理对象关系的重要概念,特别是在处理一对多(One-to-Many)或多对一(Many-to-One)关系时。 **级联操作(Cascade)** 级联操作定义了当主对象被...

    hibernate 级联查询

    Hibernate提供了级联操作来简化数据的保存、更新和删除等操作,使得开发者无需手动编写复杂的SQL语句,就能实现关联对象的联动处理。 **描述分析:** 虽然描述部分为空,但根据给定的博文链接,我们可以推测文章...

    hibernate级联增删改查

    本主题将深入探讨"hibernate级联增删改查",特别是针对"t_card主表"和"t_person从表"的关系。 首先,级联操作在Hibernate中是一种配置,用于指定当主表中的实体发生变化时,是否以及如何更新或删除关联的从表实体。...

    hibernate 级联(cascade和inverse)一对多

    一、Hibernate级联(Cascade) 级联操作允许我们将对一个实体的更改自动应用到与之关联的其他实体。在一对多关系中,如果一个实体(父实体)与多个实体(子实体)关联,设置级联属性可以确保在处理父实体时,子实体...

    hibernate many-to-many级联保存,级联更新,级联删除

    在Hibernate中,级联操作可以通过`cascade`属性进行配置。主要有以下几种级联类型: 1. `save-update`:当保存或更新父实体时,会自动保存或更新所有关联的子实体。 2. `merge`:在调用`session.merge()`时,不仅...

    Hibernate_级联关系说明_-_关于cascade和inverse的用法

    在探讨Hibernate框架中的级联操作(cascade)与控制权反转(inverse)之前,我们需要先对Hibernate有一个基本的理解。Hibernate是一个开放源代码的对象关系映射(ORM)框架,它为Java应用提供了一种将对象模型映射到...

    关于Hibernate级联删除的问题.doc

    在文档中提到的场景下,我们来详细探讨Hibernate级联删除的原理和配置方法。 首先,我们要理解关系映射中的几个关键概念。在给出的例子中,有四个表:A、AA、B和BB,它们之间存在一对多(one-to-many)和一对一...

    Hibernate 一对多、多对一、级联、加载、反转

    例如,如果在`User`和`Order`的关系中,我们希望由`User`来控制级联操作,可以将`@OneToMany`的`mappedBy`属性设在`User`上,然后设置`inverse`属性为`true`,使得`User`成为关系的主控方,这样在处理`User`时,级联...

    Hibernate的缓存级联查询

    在IT领域,尤其是在Java开发中,Hibernate是一个非常重要的对象关系映射(ORM)框架,它允许开发者用面向对象的方式操作数据库。尽管随着Spring Data JPA等现代技术的崛起,Hibernate的使用频率有所下降,但其核心...

    hibernate基础 二 关联映射 级联操作

    ### Hibernate基础之关联映射与级联操作 #### 第一部分:主键生成策略与对象状态管理 ##### 主键生成策略 在使用Hibernate框架时,合理的主键生成策略能够极大地简化开发工作并提升应用性能。Hibernate提供了多种...

    用hibernate做的省市县三级级联,

    总的来说,这个项目展示了如何使用Hibernate、Ajax和DWR来实现一个高效、用户友好的省市县三级级联选择功能。开发过程中涉及了ORM、MVC架构、数据库操作以及前端交互等多个方面,是学习和实践Web开发技能的好实例。

    java开始面试的第27天.doc

    1. Hibernate 级联操作与 `cascade` 属性: Hibernate 提供了级联操作功能,使得对一个实体的保存、更新、删除等操作可以自动影响与其关联的其他实体。在 Hibernate 中,`cascade` 属性用于指定这种级联行为。题目...

    hibernate笔记--云图智联

    ###Hibernate级联操作 级联操作包括级联添加、修改、删除和查看。在一对多或多对多关系中,配置级联类型可以控制当操作一个对象时,其关联对象的反应。例如,在一对多关系中,配置级联添加后,添加父对象时会自动为...

    Hibernate cascade (级联).doc

    ### Hibernate级联(Cascade)详解 #### 一、概述 Hibernate 是一款强大的对象关系映射 (ORM) 框架,它允许开发人员将 Java 对象模型映射到数据库表,从而极大地简化了数据访问层的开发工作。在 Hibernate 中,...

    用Struts2+Spring+Hibernate三大框架完成的级联操作

    在级联操作中,Hibernate可能被用来处理省市县和年月日的数据存取,通过HQL(Hibernate Query Language)或者Criteria API来执行SQL查询,获取级联选择的数据。 级联操作的核心在于数据间的关联,例如选择一个省后...

    JSON--List集合转换成JSON对象详解

    文中提到,直接使用fromObject(list)方法在Hibernate级联操作关联的对象上会报错,如果需要保留级联操作,就必须去掉映射文件中的级联配置。这是因为在进行JSON序列化时,Hibernate的关联关系可能会导致异常。 4. ...

    hibernate3.2的级联关系_JavaProject

    在Hibernate中,可以通过`@OneToMany`注解来定义这种关系,并通过`cascade`属性设置级联操作,例如`CascadeType.ALL`表示所有操作都将级联。 2. **一对一(OneToOne)**:这种关系意味着两个实体之间存在一对一的...

Global site tag (gtag.js) - Google Analytics