标签:ack .so public .hbm.xml ima src ring 文件 无法
多的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 }
标签:ack .so public .hbm.xml ima src ring 文件 无法
原文地址:http://www.cnblogs.com/bulrush/p/7789010.html