1、Hibernate.cfg.xml <session-factory>
        <property name="connection.username">weblogic</property> 
        <property name="connection.password">weblogic</property> 
        <property name="jndi.url">t3://127.0.0.1:7001</property>
        <property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
        <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
        <property name="connection.datasource">MYDB2</property>      
        <property name="hbm2ddl.auto">update</property>
        <property name="connection.autocommit">false</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="current_session_context_class">jta</property>            
        
        <mapping class="com.xc.pojo.Student"/>
        <mapping class="com.xc.pojo.Teacher"/>  
        <mapping class="com.xc.pojo.Teacher2"/>      
    </session-factory>2、JAVA类
public void testID2() { UserTransaction xact = null;
try {
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
xact = this.getUserTransaction();
xact.setTransactionTimeout(10000); xact.begin();
Session session1 = sf.openSession();
Teacher t2 = new Teacher();
t2.setName("T2");
t2.setTitle("2");
session1.save(t2);
System.out.println("teacher2的ID=" + t2.getId());
session1.flush();
session1.close();
xact.commit(); sf.close();
} catch (Exception e) {
e.printStackTrace();
if (xact != null) {
try {
xact.rollback();
} catch (Exception e1) {
e.printStackTrace();
}
}
}
}
当我不写session1.flush();
数据进不了数据库,不是说session1.close();就自动调用session1.flush();
的吗?

解决方案 »

  1.   

    //session1.flush(); 
    xact.commit(); //这里先提交
    session1.close(); 
      

  2.   


    当在Java SE环境中使用JTA时,默认情况下要手动flush session并且要手动关闭session.如果不想麻烦,可以设置下面的参数:hibernate.transaction.flush_before_completion 
    hibernate.transaction.auto_close_session
    close_session
      

  3.   

    是这两个:
    hibernate.transaction.flush_before_completion 
    hibernate.transaction.auto_close_session