public void saveCustomerAndOrderWithCascade() throws Exception{
// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();
Customer customer=new Customer("Tom",new HashSet());
Order order=new Order();
order.setOrderNumber("Tom_Order001"
;
order.setCustomer(customer);
customer.getOrders().add(order);
session.save(customer);
tx.commit();
;
}catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
e.printStackTrace();
} finally {
// No matter what, close the session
session.close();
}
}
Customer与Order关联关系
insert into CUSTOMER (ID,NAME)value(1,"Tome"
insert into ORDERS (ID,ORDER_NUMBER,CUSTOMER_ID) value (1,"Tom","Tom_Order001",1)
结果为何会是这样
|