Hibernate 單項多對1
阿新 • • 發佈:2017-11-06
cfg 無法 cnblogs ive per get ring mapping tab
多的1方的Java類。 把1的作為一個屬性放到多的裏面。
package com.hibernate.n21; public class Order { private Integer orderId; private String orderName; private Customer customer; private Integer getOrderId() { return orderId; } public void setOrderId(Integer 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; }public Order(String orderName, Customer customer) { super(); this.orderName = orderName; this.customer = customer; } }
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Order" table="orders" > <id name="orderId" type="java.lang.Integer"> <column name="order_Id" /> <!-- 指定主鍵的生成方式, native: 使用數據庫本地方式 --> <generator class="native" /> </id> <property name="orderName" type="java.lang.String" column="order_Name" > </property> <!-- 映射n-1的關聯關系
name:1的屬性名。把1當成多的一個屬性。
class 1的類名。
column 1的主鍵名。
--> <many-to-one name="customer" class="Customer" column="customer_Id"></many-to-one> </class> </hibernate-mapping>
1的類名:
package com.hibernate.n21; public class Customer { private Integer customerId; private String customerName; public Integer getCustomerId() { return customerId; } public void setCustomerId(Integer customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public Customer() { super(); } public Customer(String customerName) { super(); this.customerName = customerName; } }
映射文件:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.hibernate.n21"> <class name="Customer" table="customer" > <id name="customerId" type="java.lang.Integer"> <column name="customer_Id" /> <!-- 指定主鍵的生成方式, native: 使用數據庫本地方式 --> <generator class="native" /> </id> <property name="customerName" type="java.lang.String" column="customer_Name" > </property> </class> </hibernate-mapping>
然後在主的映射文件hibernate.cfg.xml文件中加上映射
<mapping resource="com/hibernate/n21/Customer.hbm.xml"/> <mapping resource="com/hibernate/n21/Order.hbm.xml"/>
SAVE():
1 先保存1的一端 只是三條insert語句。 2 @org.junit.Test 3 public void testN21Save(){ 4 Customer customer=new Customer(); 5 customer.setCustomerName("C1"); 6 Order order1 =new Order(); 7 order1.setOrderName("O1"); 8 Order order2 =new Order(); 9 order2.setOrderName("O2"); 10 11 //設置關聯關系。就是給多的一方設置外鍵 12 order1.setCustomer(customer); 13 order2.setCustomer(customer); 14 session.save(customer); 15 session.save(order1); 16 session.save(order2); 17 18 }
1 先保存多的一端。3條insert語句。2條update語句
因為在插入多的一端時,無法確定1的一端的外鍵值。所以只能先為null。等1的一端插入後。再進行修改。 2 @org.junit.Test 3 public void testN21Save(){ 4 Customer customer=new Customer(); 5 customer.setCustomerName("C1"); 6 Order order1 =new Order(); 7 order1.setOrderName("O1"); 8 Order order2 =new Order(); 9 order2.setOrderName("O2"); 10 11 //設置關聯關系。就是給多的一方設置外鍵 12 order1.setCustomer(customer); 13 order2.setCustomer(customer); 14 15 session.save(order1); 16 session.save(order2); 17 session.save(customer); 18 }
Hibernate 單項多對1