主要代码:
package mypack;import net.sf.hibernate.*;
import net.sf.hibernate.cfg.Configuration;
import java.util.*;public class BusinessService{
  public static SessionFactory sessionFactory;
  static{
     try{
      // Create a configuration based on the properties file we've put
       Configuration config = new Configuration();
       config.addClass(Customer.class)
             .addClass(Order.class);
      // Get the session factory we can use for persistence
      sessionFactory = config.buildSessionFactory();
    }catch(Exception e){e.printStackTrace();}
  } 
  
    public void associateCustomerAndOrder() 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=(Customer)session.load(Customer.class,new Long(2));
      Order order=(Order)session.load(Order.class,new Long(2));
      order.setCustomer(customer);
      customer.getOrders().add(order);
      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();
    }
  }  public void saveCustomerAndOrderSeparately() 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();
      customer.setName("Jack");      Order order=new Order();
      order.setOrderNumber("Jack_Order001");      session.save(customer);
      session.save(order);
      // We're done; make our changes permanent
      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();
    }
  }   public void saveCustomerAndOrderWithInverse()throws Exception{
      saveCustomerAndOrderSeparately();
      associateCustomerAndOrder();
   }   public void test() throws Exception{
      saveCustomerAndOrderWithInverse();
  }  public static void main(String args[]) throws Exception {
    new BusinessService().test();
    sessionFactory.close();
  }
}

解决方案 »

  1.   

    其中saveCustomerAndOrderWithInverse();分别调用了 saveCustomerAndOrderSeparately();
    //持久化2个独立的对象customer和order
    associateCustomerAndOrder();
    //将2个对象关联我想请问为什么将inverse设置卫true的时候就只发送了一条update语句,而设置为false的时候就发送了2个,请各位大侠给我详细解释一下inverse的使用
      

  2.   

    inverse一般是在关联关系中,一对多,多对一的双向关联关系中的“一”这一端中设置的。
    它的作用就是把操作数据表的动作交给对方去维护。
    设置inverse="true" 执行saveCustomerAndOrderWithInverse动作就交给Order这一端去做啦。
    具体的你要去看看,书上的mapping工作原理了。