Hibernate入门04_双向多对一
来源:互联网 发布:玛雅动漫制作软件 编辑:程序博客网 时间:2024/06/06 11:02
这里还是以顾客和订单之间的关系来说明,一个客户可以有多个订单,但是一个订单只能对应一个客户;
1、项目的目录结构
2、Customer.java
import java.util.HashSet;import java.util.Set;/**客户-可以有多个 */public class Customer { private Integer customerId; private String customerName; /**需要把集合初始化防止发生null指针异常*/ private Set<Order> orders = new HashSet<>(); //get/set...}
Customer.hbm.xml映射文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2017-3-25 18:05:11 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="com.hibernate.entities.n21both"> <class name="Customer" table="CUSTOMERS"> <id name="customerId" type="java.lang.Integer"> <column name="CUSTOMER_ID" /> <generator class="native" /> </id> <property name="customerName" type="java.lang.String"> <column name="CUSTOMER_NAME" /> </property> <!-- 映射一对多的集合映射 set的三个属性: inverse="true":放弃维护关联关系,让Order来维护, cascade="delete":设置级联关系删除 , cascade="delete-orphan";开发是不建议设置cascade,建议手动删除 order-by:在查询时对集合中的元素进行排序 --> <set name="orders" table="ORDERS" inverse="true" order-by="ORDER_NAME DESC"> <key column="CUSTOMER_ID"></key> <one-to-many class="Order" /> </set> </class></hibernate-mapping>
3、Order.java
public class Order { private Integer orderId; private String orderName; private Customer customer; //set/get...}
Order.hbm.xml映射文件
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2017-3-25 18:05:11 by Hibernate Tools 3.4.0.CR1 --><hibernate-mapping package="com.hibernate.entities.n21both"> <class name="Order" table="ORDERS"> <id name="orderId" type="java.lang.Integer"> <column name="ORDERID" /> <generator class="native" /> </id> <property name="orderName" type="java.lang.String"> <column name="ORDER_NAME" /> </property> <!-- 映射多对一的关联关系 使用多对一的关联关系 --> <many-to-one name="customer" class="Customer" column="CUSTOMER_ID"></many-to-one> </class></hibernate-mapping>
4、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="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate01</property> <!-- hibernate的基本信息 --> <!-- hibernate使用的数据库方言 --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 执行操作时是否在控制台打印sql语句 --> <property name="show_sql">true</property> <!-- 是否对sql进行格式化 --> <property name="format_sql">true</property> <!-- 指定自动生成数据表的策略 --> <property name="hbm2ddl.auto">update</property> <!-- 设置hibernate的隔离级别 --> <property name="connection.isolation">2</property> <!-- 删除对象后,设置其OID为null --> <property name="use_identifier_rollback">true</property> <!-- 配置C3P0数据源 --> <property name="c3p0.max_size">100</property> <property name="c3p0.min_size">5</property> <property name="c3p0.acquire_increment">2</property> <property name="c3p0.idle_test_period">2000</property> <property name="c3p0.timeout">2000</property> <property name="c3p0.max_statements">10</property> <!-- 设置JDBC的statement读取数据库的时候每次从数据库中读取记录条数,这个值100合适,太大会损耗内存,小了速度慢 --> <property name="jdbc.fetch_size">100</property> <!-- 设置数据库进行批量删除,更新,插入的时候批次的大小,30比较合适 --> <property name="jdbc.batch_size">30</property> <!-- 指定关联的hbm.xml文件 --> <mapping resource="com/hibernate/entities/n21both/Order.hbm.xml"/> <mapping resource="com/hibernate/entities/n21both/Customer.hbm.xml"/> </session-factory></hibernate-configuration>
5、Test测试
import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.service.ServiceRegistry;import org.hibernate.service.ServiceRegistryBuilder;import org.junit.After;import org.junit.Before;import org.junit.Test;import com.hibernate.entities.n21both.Customer;import com.hibernate.entities.n21both.Order;public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Test public void testOne2ManySave() { Customer customer = new Customer(); customer.setCustomerName("AA"); Order order1 = new Order(); order1.setOrderName("Oyder1"); Order order2 = new Order(); order2.setOrderName("Oyder2"); // 设定关联关系 order1.setCustomer(customer); order2.setCustomer(customer); customer.getOrders().add(order1); customer.getOrders().add(order2); // 三条insert两条update session.save(customer); session.save(order1); session.save(order2); // 三条insert,4条update // session.save(order1); // session.save(order2); // session.save(customer); } @Test public void testOne2ManyGet() { // 对多的一端采用延迟加载 Customer customer = (Customer) session.get(Customer.class, 1); System.out.println(customer.getCustomerName()); System.out.println(customer.getOrders().getClass()); } @Test public void testOne2ManyDelete(){ //设定级联 cascade="delete":设置级联关系删除关系后,可以删除 Customer customer = (Customer) session.get(Customer.class, 1); session.delete(customer); } @Test public void testCascade_Orphan(){ //设定级联 cascade="delete-orphan":设置级联关系删除关系后,可以删除 Customer customer = (Customer) session.get(Customer.class, 2); customer.getOrders().clear();// session.delete(customer); } @Before public void init() { System.out.println("HibernateTest.init()"); // 1.创建一个sessionFactory对象 Configuration configuration = new Configuration().configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()) .buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); // 2.创建一个session对象 session = sessionFactory.openSession(); // 3.开启事务 transaction = session.beginTransaction(); } @After public void destroy() { System.out.println("HibernateTest.destroy()"); // 5.提交事务 transaction.commit(); // 6.关闭session session.close(); // 7.关闭SessionFactory sessionFactory.close(); }}
6、备注:本博客适合有一定java基础,对数据库有一定认识的读者。不会做过多的文字描述,敬请原谅。本人也是新手,写博客当做笔记作分享,不喜勿喷,欢迎指教。
阅读全文
0 0
- Hibernate入门04_双向多对一
- Hibernate关联映射_双向多对一
- Hibernate入门4_多对一关联映射
- Hibernate入门03_单向多对一
- 【hibernate实例】赵雅智_双向多对一客户与订单增加查找修改
- Hibernate双向一对多(多对一)
- 五、hibernate多对一(双向关联)
- Hibernate Annotation 多对一 双向
- Hibernate双向多对一关系
- hibernate双向多对一映射关系
- hibernate ORM映射:双向多对一
- Hibernate双向多对一关系
- Hibernate学习笔记----双向多对一
- hibernate 多对一的双向理解
- Hibernate双向多对一映射
- Hibernate双向多对一(注解)
- Hibernate双向多对一实战
- Hibernate 06 : 双向多对一
- Rsync同步算法
- JSTL标准标签库之格式化标签(三)
- LuoguP2254 瑰丽华尔兹
- Fibonacci
- MySQL联合查询分页
- Hibernate入门04_双向多对一
- 自定义类加载器
- Fiddler(一)模拟弱网环境
- CView
- 数组的反序用临时变量
- JDBC了解(4)->元数据
- 夏天的羊肉汤
- 【51Nod 1184】第N个质数
- Android四大组件之--Activity的深入浅出