1. what is correlation (association)
1.1 Association refers to the reference relationship between classes. If class A is associated with class B, the referenced class B will be defined as the property of class A.
For example:
t_hibernate_order
t_hibernate_order_item
An order corresponds to multiple orders
class Order{ private List<OrderItem> orderItems; } class OrderItem{ private Order order; } class B{ private String name; } public class A{ private B b = new B; public A(){} }
hibernate:orm Framework, Operating Database with Object-Oriented Thought
Entity Class - Table
Attribute - field
1.2 Classification of associations: associations can be divided into one-to-one, one-to-many/many-to-one, and many-to-many associations
Relevance is directional
Order Form, Order Item Form select * from t_order t,t_orderItem m where t.oid = m.oid and ..... class Order{ private STIRNG OID; ... private Set<OrderItem> OrderItems; } session.get(Order.class,1)
Be careful:
1. The key points are all on the foreign keys in the database. Please understand the following two sentences of SQL and the one-to-many and many-to-one relationship.
2. select * from Orders where cid=?// This SQL returns 0-n orders corresponding to the customer.
3. select * from Customers where customerId=?//This SQL returns one customer corresponding to the order.
4. Through these two SQL, it is not difficult to see that foreign keys have two purposes here: to find customers'orders, to find customers corresponding to orders.
Entity bag:
Establish one-to-many relationship between customers and orders
<set lazy="true" name="orderItems" cascade="save-update" inverse="true"> <key column="oid"></key> <one-to-many class="com.javaxl.three.entity.OrderItem" /> </set>
Order.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.qiuwenfan.three.entity.Order" table="t_hibernate_order"> <id name="orderId" type="java.lang.Integer" column="order_id"> <generator class="increment" /> </id> <property name="orderNo" type="java.lang.String" column="order_no"> </property> <!-- bag Label name:Association attributes of classes currently to be mapped cascade:Maintaining relationships inverse:Decide who will maintain the relationship key Label column:Column segments corresponding to foreign keys of tables one-to-many class:Full path names of classes corresponding to associated attributes --> <bag name="orderItems" cascade="save-update" inverse="true"> <key column="oid"></key> <one-to-many class="com.qiuwenfan.three.entity.OrderItem"></one-to-many> </bag> </class> </hibernate-mapping>
Order.java:
package com.qiuwenfan.three.entity; import java.util.ArrayList; import java.util.List; public class Order { private Integer orderId; private String orderNo; private List<OrderItem> orderItems = new ArrayList<OrderItem>(); private Integer initChildren = 0;//0 stands for default lazy loading and 1 for mandatory loading public Integer getInitChildren() { return initChildren; } public void setInitChildren(Integer initChildren) { this.initChildren = initChildren; } public Integer getOrderId() { return orderId; } public void setOrderId(Integer orderId) { this.orderId = orderId; } public String getOrderNo() { return orderNo; } public void setOrderNo(String orderNo) { this.orderNo = orderNo; } public List<OrderItem> getOrderItems() { return orderItems; } public void setOrderItems(List<OrderItem> orderItems) { this.orderItems = orderItems; } @Override public String toString() { return "Order [orderId=" + orderId + ", orderNo=" + orderNo + "]"; } }
OrderItem.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.qiuwenfan.three.entity.OrderItem" table="t_hibernate_order_item"> <id name="orderItemId" type="java.lang.Integer" column="order_item_id"> <generator class="increment" /> </id> <property name="productId" type="java.lang.Integer" column="product_id"> </property> <property name="quantity" type="java.lang.Integer" column="quantity"> </property> <property name="oid" type="java.lang.Integer" column="oid" insert="false" update="false"> </property> <!-- many-to-one : Multi-to-one configuration (because multiple orders correspond to one order) name : Refers to the class currently being mapped com.qiuwenfan.three.entity.OrderItem Relevant attributes order class : Refers to the category of the associated attribute (full path name) column : Here's the foreign key. --> <many-to-one name="order" class="com.qiuwenfan.three.entity.Order" column="oid"></many-to-one> </class> </hibernate-mapping>
OrderItem.java:
package com.qiuwenfan.three.entity; public class OrderItem { private Integer orderItemId; private Integer productId; private Integer quantity; private Integer oid; private Order order; public Integer getOrderItemId() { return orderItemId; } public void setOrderItemId(Integer orderItemId) { this.orderItemId = orderItemId; } public Integer getProductId() { return productId; } public void setProductId(Integer productId) { this.productId = productId; } public Integer getQuantity() { return quantity; } public void setQuantity(Integer quantity) { this.quantity = quantity; } public Integer getOid() { return oid; } public void setOid(Integer oid) { this.oid = oid; } public Order getOrder() { return order; } public void setOrder(Order order) { this.order = order; } @Override public String toString() { return "OrderItem [orderItemId=" + orderItemId + ", productId=" + productId + ", quantity=" + quantity + ", oid=" + oid + "]"; } }
Dao bag:
DemoDao.java:
package com.qiuwenfan.three.dao; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import com.qiuwenfan.three.entity.Order; import com.qiuwenfan.three.entity.OrderItem; import com.qiuwenfan.two.util.SessionFactoryUtils; public class DemoDao { /** * To test for accurate configuration of relational mapping files * Explain the use of insert=false, update=false * @param order * @return */ public Integer addOrder(Order order) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Integer oid = (Integer)session.save(order); transaction.commit(); session.close(); return oid; } public Integer addOrderItem(OrderItem orderItem) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Integer otid = (Integer)session.save(orderItem); transaction.commit(); session.close(); return otid; } /** * To explain the problem of lazy loading (all queries after hibernate 3.0 are lazy loading by default) * 1,There is a problem when looking up a single proxy object. The proxy object is closed. * 2,Look for multiple problems, performance problems * @param order * @return */ public Order getOrder(Order order) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Order o = session.get(Order.class, order.getOrderId()); if(o != null && new Integer(1).equals(order.getInitChildren())) { Hibernate.initialize(o.getOrderItems()); // System.out.println(o.getOrderItems()); } transaction.commit(); session.close(); return o; } public List<Order> getOrderList() { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); List<Order> list = session.createQuery("from Order").list(); transaction.commit(); session.close(); return list; } /** * z The data of the main table can not be deleted casually. The corresponding information in the slave table must be deleted before the information of the main table can be deleted. * @param order */ public void delOrder(Order order) { Session session = SessionFactoryUtils.openSession(); Transaction transaction = session.beginTransaction(); Order order2 = session.get(Order.class, order.getOrderId()); for (OrderItem oi : order2.getOrderItems()) { session.delete(oi); } session.delete(order2); // session.delete(order); transaction.commit(); session.close(); } }
DemoDaoTest.java:
package com.qiuwenfan.three.dao; import static org.junit.Assert.*; import java.util.List; import org.junit.Test; import com.qiuwenfan.three.entity.Order; import com.qiuwenfan.three.entity.OrderItem; public class DemoDaoTest { private DemoDao demoDao = new DemoDao(); /** * Order, order items added at the same time */ @Test public void testAddOrder() { Order order = new Order(); order.setOrderNo("Happy shopping"); for (int i = 0; i < 6; i++) { OrderItem oi = new OrderItem(); oi.setProductId(10+i); oi.setQuantity(20+i); // demoDao.addOrderItem(oi); order.getOrderItems().add(oi); oi.setOrder(order); } demoDao.addOrder(order); } /** * Add order */ @Test public void testAddOrderItem() { Order order = new Order(); OrderItem oi = new OrderItem(); oi.setProductId(66); oi.setQuantity(66); order.setOrderId(11); oi.setOrder(order); demoDao.addOrderItem(oi); } /** * Lazy loading */ @Test public void testGetOrder() { Order order = new Order(); order.setOrderId(11); order.setInitChildren(1); Order o = demoDao.getOrder(order); System.out.println(o.getOrderNo()); System.out.println(o.getOrderItems().size()); } /** * Query single */ @Test public void testGetOrderList() { List<Order> orderList = demoDao.getOrderList(); for (Order order : orderList) { System.out.println(order.getOrderNo()); // System.out.println(order.getOrderItems().size()); } } /** * Delete orders and corresponding order items */ @Test public void testDelOrder() { Order order = new Order(); order.setOrderId(11); this.demoDao.delOrder(order); } }
Hibernate.cfg.xml configuration:
<!-- One-to-many explanation --> <mapping resource="com/qiuwenfan/three/entity/OrderItem.hbm.xml" /> <mapping resource="com/qiuwenfan/three/entity/Order.hbm.xml" />
New orders and items:
Query orders (lazy loading):
Delete the order and the corresponding order item: