Hibernate入门03_单向多对一
来源:互联网 发布:淘宝怎么联系不了卖家 编辑:程序博客网 时间:2024/06/14 13:26
这里用一个顾客对应多个订单为例:
1、Customer.java
/**客户-可以有多个 */ public class Customer { private Integer customerId; private String customerName; //set/get... }
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> <class name="com.hibernate.entities.n21.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> </class></hibernate-mapping>
2、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.n21"> <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>
3、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/n21/Order.hbm.xml"/> <mapping resource="com/hibernate/entities/n21/Customer.hbm.xml"/> </session-factory></hibernate-configuration>
4、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.n21.Customer;import com.hibernate.entities.n21.Order;public class HibernateTest { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Test public void testN21Save() { Customer customer = new Customer(); customer.setCustomerName("BB"); Order order1 = new Order(); order1.setOrderName("Oyder3"); order1.setCustomer(customer); Order order2 = new Order(); order2.setOrderName("Oyder4"); order2.setCustomer(customer); // 注意插入顺序先插入1的一端,在插入n的一端,会发生3条insert语句 // session.save(customer); // session.save(order1); // session.save(order2); // 注意插入顺序,先插入n的一端,在插入1的一端,会发生3条insert语句,2条update语句,因为无法确定1的一端,所以会发生两条update语句,不推荐这样 session.save(order1); session.save(order2); session.save(customer); } @Test public void testn21Update() { Order order = (Order) session.get(Order.class, 1); order.getCustomer().setCustomerName("AAA"); } @Test public void testn21Delete() { Customer customer = (Customer) session.get(Customer.class, 1); // 在不设定级联关系的情况下,不能直接删除1这一端的对象,因为在其他表中有其外键引用 session.delete(customer); } @Test public void testN21Get() { // 1.若查询多的一端的一个对象,默认情况下只查询对的一端的对象,而没有查询关联1的那一段的对象 Order order = (Order) session.get(Order.class, 1); Customer customer = order.getCustomer(); // 2.在需要使用到关联的对象时才去查询 System.out.println(customer.getCustomerName()); } @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(); }}
5、备注:本博客适合有一定java基础,对数据库有一定认识的读者。不会做过多的文字描述,敬请原谅。本人也是新手,写博客当做笔记作分享,不喜勿喷,欢迎指教。
阅读全文
0 0
- Hibernate入门03_单向多对一
- Hibernate关系映射(7)_多对一单向关联
- hibernate 单向多对一
- Hibernate多对一单向关联映射
- hibernate单向多对一关联
- Hibernate多对一单向关联
- hibernate:多对一单向关联映射
- hibernate多对一单向关联
- Hibernate多对一单向关联
- hibernate多对一单向关联_Annotation
- hibernate多对一单向关联_XML
- Hibernate学习:单向多对一关联
- Hibernate多对一映射单向关联
- hibernate多对一单向配置
- hibernate的单向多对一
- Hibernate Annotation 多对一单向关联
- Hibernate多对一关联映射(单向)
- hibernate多对一单向关联
- 集合知识点归纳
- JDBC了解(3)-beautils
- IO多路复用--select和epoll详解
- 一句总结设计模式
- C#学习笔记:动态设置控件的背景图片
- Hibernate入门03_单向多对一
- 物理学史二十讲
- [一天几个linux命令] 目录与文件的权限意义
- linux安装apache
- 编程书籍推荐
- 学习淘淘商城第七十九课(LVS+Nginx负载均衡----下篇)
- Python面向对象——01
- C语言中 float double在内存中的存储
- poj3558 euler