1. What is the association (Association)
1.1 association refers references between classes. If the class associated with the class A B, then the referenced class B to class A are defined as attributes of. For example:
Classification 1.2 association: association can be divided into one-to-many / many-to-many associations,
New Entity class relationships: Order
com.zl.three.entity Package Penalty for; Import java.util.ArrayList; Import java.util.List; public class the Order { private Integer orderId; private String orderNo; // establish a relationship corresponding to the order of a multiple line items private List <the OrderItem> = new new the OrderItems the ArrayList <> (); Private initChildren Integer = 0; // lazy loading public getInitChildren Integer () { return initChildren; } public void setInitChildren (Integer initChildren) { this.initChildren = initChildren; } public List <the OrderItem> getOrderItems () { return the orderItems; } public void setOrderItems (List <the OrderItem> orderItems) { the orderItems = orderItems; } 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; } @Override public String toString() { return "Order [orderId=" + orderId + ", orderNo=" + orderNo + "]"; } }
Maintenance of relationships in the mapping file
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.zl.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标签: lazy:Whether lazy loading, lazy loading of default is true lazy mode inverse: relationship to control the other party default is true, the current class does not maintain a relationship cascade: cascade to cascade new and modified name: attribute name associated with the class sub-tab key: column: the primary key in the primary table from the foreign key table subtag One-to-MANY: class: foreign key corresponding entity class -> <Bag the lazy = "to false" name = "orderItems" Cascade = "Save-Update" inverse = "to true"> <Key column = "OID"> </ Key> <One-to-MANY class = "com.zl.three.entity.OrderItem" /> </ Bag> </ class> </ hibernate- mapping>
OrderItem
com.zl.three.entity Package; public class the OrderItem { Private Integer OrderItemId; Private Integer the productId; Private Quantity Integer; Private OID Integer; // build a relationship is a line corresponding to the entry order Private the Order Order; public getOrder the Order ( ) { return Order; } public void the setOrder (the Order Order) { this.order = Order; } public getOrderItemId Integer () { return OrderItemId; } public void setOrderItemId (Integer OrderItemId) { this.orderItemId = OrderItemId; } public Integer getProductID ( ) { return the 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; } @Override public String toString() { return "OrderItem [orderItemId=" + orderItemId + ", productId=" + productId + ", quantity=" + quantity + ", oid=" + oid + "]"; } }
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.zl.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 name="order" class="com.zl.three.entity.Order" column="oid"> </many-to-one> </class> </hibernate-mapping>
hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!-- 1. 数据库相关 --> <property name="connection.username">root</property> <property name="connection.password">123</property> <property name="connection.url">jdbc:mysql://localhost:3306/db_0601?useUnicode=true&characterEncoding=UTF-8 </property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <Property name = "hbm2ddl.auto"> Update </ Property> <-! Configure Local Affairs (No CurrentSessionContext the Configured!) -> <Property name = "hibernate.current_session_context_class"> the Thread </ Property> <! - 2. commissioning -> <Property name = "show_sql"> to true </ Property> <Property name = "format_sql"> to true </ Property> <-! 3. Add entity mapping file -> <Resource mapping = "COM / ZL / One / Entity / User.hbm.xml" /> <-! primary key generation strategy -> <Mapping Resource = "COM / ZL / TWO / Entity / Worker.hbm.xml" /> <Mapping = Resource "COM / ZL / TWO / Entity / Student.hbm.xml" /> <-! many -> <mapping resource="com/zl/three/entity/OrderItem.hbm.xml" /> <= Resource Mapping "com/zl/three/entity/Order.hbm.xml" /> </session-factory> </hibernate-configuration>
Test: DemoDao
package com.zl.three.DemoDao; import java.util.List; import org.hibernate.Hibernate; import org.hibernate.Session; import org.hibernate.Transaction; import com.zl.three.entity.Order; import com.zl.three.entity.OrderItem; import com.zl.two.util.SessionFactoryUtils; public class DemoDao { /** * 为了测试关系型映射文件配置准确 * 讲解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 (the OrderItem orderItem) { the Session SessionFactoryUtils.openSession the session = (); the Transaction Transaction session.beginTransaction = (); Integer otid = (Integer) the session. Save (orderItem); transaction.commit (); Session.close (); return otid; } / ** * in order to explain the problems of lazily (after all hibernate3.0 query is used by default lazy loading) * 1, single check if a problem exists, the proxy object has been closed * 2, a plurality of check problems, there are performance problems * @param Order * @return * / public getOrder the Order (the Order Order) { the Session SessionFactoryUtils.openSession the session = (); the Transaction Transaction session.beginTransaction = (); the Order Session.get = O (the Order. class, order.getOrderId ()); IF (! = null && O new new Integer (. 1) .equals (order.getInitChildren ())) { // load the associated force objects Hibernate.initialize (o.getOrderItems ()); // System.out.println (O. getOrderItems ()); } transaction.commit (); Session.close (); return O; } public List <the Order> getOrderList () { the Session SessionFactoryUtils.openSession the session = (); the Transaction Transaction session.beginTransaction = (); List . <the Order> = session.createQuery List ( "from the Order") List (); transaction.commit (); Session.close (); return List; } /** * z master data table can not be deleted, must first be removed from the table corresponding information in order to delete the information of the main table. @Param the Order * * / public void delOrder (the Order Order) { the Session SessionFactoryUtils.openSession the session = (); the Transaction Transaction session.beginTransaction = (); the Order Order2 = Session.get (Order.class, order.getOrderId ()); for ( OI the OrderItem: order2.getOrderItems ()) { Session.delete (OI); } Session.delete (Order2); // Session.delete (Order); transaction.commit (); Session.close (); } }
Test DemoDaoTest
com.zl.three.DemoDao Package; Import java.util.List; Import org.junit.After; Import org.junit.Before; Import org.junit.Test; Import com.zl.three.entity.Order; Import COM ; .zl.three.entity.OrderItem / ** performed once before each test * @Before a test method @Test mark will be called to perform after each test * test @After a @Test mark, will be called once * @ ZL author * * / public class DemoDaoTest { Private DemoDao demoDao new new DemoDao = (); / * the Before @ public void the setUp () throws Exception { } @After public void the tearDown () throws Exception { } * / @Test public void testAddOrder ( ) { the Order Order the Order new new = (); order.setOrderNo ( "p20"); OrderItem orderItem = new OrderItem(); for (int i = 0; i < 6; i++) { orderItem = new OrderItem(); orderItem.setProductId(10+i); orderItem.setQuantity(20+i); //维护关联关系 orderItem.setOrder(order); order.getOrderItems().add(orderItem); } demoDao.addOrder(order); } @Test public void testAddOrderItem() { OrderItem orderItem = new OrderItem(); orderItem.setProductId(22); orderItem.setQuantity(11); Order order = new Order(); order.setOrderId(2); order.getOrderItems().add(orderItem); orderItem.setOrder(order ); demoDao.addOrderItem (orderItem); } @Test List < the Order> OrderList this.demoDao.getOrderList = (); for (the Order Order: OrderList) { public void testGetOrder() { = New new the Order Order the Order (); order.setOrderId (. 1); // order.setInitChildren (. 1); the Order Order2 = this.demoDao.getOrder (Order); // time when the lazy = false, execution will complete hibernate two operations, session will be closed // for performance reasons, lazy with this attribute appear hibernate3.0, that is to say it is loaded associated attributes // List <OrderItem> orderItems = order2.getOrderItems (); // for ( orderItem the orderItem: orderItems) { // System.out.println (orderItem); //} System.out.println (Order2); } @Test public void testGetOrderList () { for (the orderItem orderItem: order.getOrderItems ()) { System.out.println (orderItem); } System.out.println (Order); } } @Test public void testDelOrder() { Order order = new Order(); order.setOrderId(2); this.demoDao.delOrder(order); } }