Hibernate 之many-to-one 单向多对一关联示例

Customer.hbm.xml

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.liuxi.examples.Customer" table="CUSTOMERS" schema="HIBERNATE"> <id name="id" column="ID" type="java.lang.Long"> <generator class="increment"/> </id> <property name="name" column="NAME" type="java.lang.String"/> </class> </hibernate-mapping>

 

Order.hbm.xml:

<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.liuxi.examples.Order" table="ORDERS" schema="HIBERNATE"> <id name="id" column="ID" type="java.lang.Long"> <generator class="increment"/> </id> <property name="orderNumber" column="ORDER_NUMBER" type="java.lang.String"/> <many-to-one name="customer" column="CUSTOMER_ID" class="com.liuxi.examples.Customer" not-null="true" cascade="save-update"/> </class> </hibernate-mapping>

 

 

Customer.java

 

package com.liuxi.examples; import java.util.HashSet; import java.util.Set; import com.sun.org.apache.bcel.internal.generic.NEW; public class Customer { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }

 

Order.java

package com.liuxi.examples; public class Order { private Long id; private String orderNumber; private Customer customer; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getOrderNumber() { return orderNumber; } public void setOrderNumber(String orderNumber) { this.orderNumber = orderNumber; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }

 

 

测试程序:

 

 

package com.liuxi.service; import org.hibernate.HibernateException; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.classic.Session; import com.liuxi.examples.Customer; import com.liuxi.examples.HibernateSessionFactoryUtil; import com.liuxi.examples.Order; public class BusinessService { public static void main(String[] args) { saveOrder(); } public static void saveOrder(){ SessionFactory sessionFactory=HibernateSessionFactoryUtil.getSessionFactory(); Session session=sessionFactory.getCurrentSession(); Transaction tx=null; try { tx=session.beginTransaction(); Customer c1=new Customer(); c1.setName("赵云"); // session.save(c1); Order o1=new Order(); o1.setOrderNumber("赵云_Order0001"); o1.setCustomer(c1); Order o2=new Order(); o2.setOrderNumber("赵云_Order0002"); o2.setCustomer(c1); session.save(o1); session.save(o2); tx.commit(); } catch (HibernateException e) { e.printStackTrace(); if(tx!=null){ tx.rollback(); } } } }

 

 

 

 

当not-null为true,且cascade为none时,不session.save(c)就session.save(order)会抛异常。这种原因是什么呢?很值得研究一下

你可能感兴趣的:(Hibernate,MyEclipse,String,Class,generator,encoding)