1 .单向 n-1 关联只需从n的一端可以访问1的一端
2.域模型: 从Order到Customer的多对一单向关联需要在Order类中定义一个Customer属性,而在Customer类中无需定义存放Order对象的集合属性
3.关系数据模型:ORDERS表中的CUSTOMER_ID参照CUSTOMER表的主键
测试代码:
1.Customer实体类和Order实体类
package com.elgin.hibernate.nto1; public class Customer { private int customerId; private String customerName; public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } }
package com.elgin.hibernate.nto1; public class Order { private int orderId; private String orderName; private Customer customer; public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public String getOrderName() { return orderName; } public void setOrderName(String orderName) { this.orderName = orderName; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }2.2个实体类分别对应的hbm映射文件
<?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 2015-9-21 20:58:02 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="com.elgin.hibernate.nto1.Customer" table="CUSTOMERS"> <id name="customerId" type="int"> <column name="CUSTOMER_ID" /> <generator class="native" /> </id> <property name="customerName" type="java.lang.String"> <column name="CUSTOMER_NAME" /> </property> </class> </hibernate-mapping>
<?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 2015-9-21 20:58:02 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping package="com.elgin.hibernate.nto1"> <class name="Order" table="ORDERS"> <id name="orderId" type="int"> <column name="ORDER_ID" /> <generator class="native" /> </id> <property name="orderName" type="java.lang.String"> <column name="ORDER_NAME" /> </property> <!-- 映射多对一关联关系 使用many-to-one映射多对一关联关系 name:多这一端关联一的那一端的属性名 class:一那一端对应的类名 column:一那一端对应多的一端对应的数据表中字段的名字 --> <many-to-one name="customer" class="Customer"> <column name="CUSTOMER_ID" /> </many-to-one> </class> </hibernate-mapping>3.Junit单元测试类
@Test public void testMany2OneSave(){ Customer customer=new Customer(); customer.setCustomerName("CC"); Order order1=new Order(); order1.setOrderName("order-5"); order1.setCustomer(customer); Order order2=new Order(); order2.setOrderName("order-6"); order2.setCustomer(customer); //发出了3条insert语句 //先插入1的一端,再插入n的一端,只有insert语句 /*session.save(customer); session.save(order1); session.save(order2);*/ //发出了3条insert语句和2条update语句 //先插入n的一端,再插入1的一端,要额外发出update语句,推荐使用第一种方式 session.save(order1); session.save(order2); session.save(customer); } @Test public void testMany2OneGet(){ //1.若查询的n的那一端的对象,默认情况下,只查询n的一端的对象, //2.它所关联的1的那一端的对象为代理对象,等到使用的时候才会发出sql语句查询对此代理对象进行初始化 //3.如果在使用此对象时session已关闭,则会抛出懒加载异常LazyInitializationException Order order=(Order) session.get(Order.class, 1); System.out.println(order.getOrderName()); System.out.println(order.getCustomer().getCustomerName()); } @Test public void testMany2OneDelete(){ //在不设定关联关系的前提下,如果1的一端有对象被n一端的对象引用,不能直接删除1一端的对象 Customer customer=(Customer) session.get(Customer.class, 1); session.delete(customer); }