999yzc亚洲城在线娱乐城hibernate多对多问题

( 999yzc亚洲城在线娱乐城 )

映射文件如下:

<hibernate-mapping>
  <class name="com.bdqn.entity.Student" table="STUDENT">
      <id name="id" type="java.lang.Integer" column="STUID">
        <generator class="sequence">
          <param name="sequence">sequence_stuid</param>
        </generator>
      </id>
      <property name="name" type="java.lang.String" column="STUNAME" />
      
      <set name="teacherSet" table="TEASTU" cascade="save-update">
        <key column="MSTUID" />
        <many-to-many class="com.bdqn.entity.Teacher" column="MTEAID" />
      </set>
  </class>
</hibernate-mapping>
<hibernate-mapping>
  <class name="com.bdqn.entity.Teacher" table="TEACHER">
    <id name="id" type="java.lang.Integer" column="TEAID">
      <generator class="sequence">
        <param name="sequence">sequence_teaid</param>
      </generator>
    </id>
    <property name="name" type="java.lang.String" column="TEANAME" />
    
    <set name="studentSet" table="TEASTU" inverse="true">
      <key column="MTEAID" />
      <many-to-many class="com.bdqn.entity.Student" column="MSTUID" />
    </set>
  </class>
</hibernate-mapping>


@Test
  public void testSave2 {
    Configuration cfg = null;
    ServiceRegistry sr = null;
    SessionFactory sf = null;
    Session session = null;
    Transaction tx = null;
    try {
      cfg = new Configuration.configure"hibernate.cfg.xml";
      sr = new StandardServiceRegistryBuilder.applySettingscfg.getProperties.build;
      sf = cfg.buildSessionFactorysr;
      System.out.println"SessionFactory --> " + sf;
      session = sf.openSession;
      System.out.println"SESSION --> " + session;
      tx = session.beginTransaction;
      // 创建对象
      Teacher t1 = new Teacher"韩老师";
      Teacher t2 = new Teacher"王老师";
      Student s1 = new Student"小明";
      Student s2 = new Student"小蓝";
      // 建立关联关系
      t1.getStudentSet.adds1;
      t1.getStudentSet.adds2;
      t2.getStudentSet.adds2;

      s1.getTeacherSet.addt1;
      s2.getTeacherSet.addt1;
      s2.getTeacherSet.addt2;
      
      // 由主动方维护关联关系 inverse="false"
      session.saves1;
      session.saves2;
      
      tx.commit;
      System.out.println"transaction commit.";
      
    } catch Exception e {
      tx.rollback;
      System.out.println"transaction rollback.";
    } finally {
      if session != null session.close;
      System.out.println"session closed.";
    }
    
  }
  

执行以上方法时事务为什么不会提交?
下面是控制台打印的信息


八月 22, 2015 6:49:48 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final}
八月 22, 2015 6:49:48 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.10.Final}
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: hibernate.cfg.xml
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: hibernate.cfg.xml
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/bdqn/entity/Student.hbm.xml
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/bdqn/entity/Teacher.hbm.xml
八月 22, 2015 6:49:48 下午 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
八月 22, 2015 6:49:48 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH000402: Using Hibernate built-in connection pool not for production use!
八月 22, 2015 6:49:48 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000401: using driver [oracle.jdbc.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
八月 22, 2015 6:49:48 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000046: Connection properties: {user=rolland, password=**}
八月 22, 2015 6:49:48 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH000006: Autocommit mode: false
八月 22, 2015 6:49:48 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 20 min=1
八月 22, 2015 6:49:49 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
八月 22, 2015 6:49:49 下午 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy direct JDBC transactions
八月 22, 2015 6:49:49 下午 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
SessionFactory –> org.hibernate.internal.SessionFactoryImpl@52815fa3
SESSION –> SessionImplPersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@5e403b4a updates=org.hibernate.engine.spi.ExecutableList@5117dd67 deletions=org.hibernate.engine.spi.ExecutableList@5be49b60 orphanRemovals=org.hibernate.engine.spi.ExecutableList@2931522b collectionCreations=org.hibernate.engine.spi.ExecutableList@7674b62c collectionRemovals=org.hibernate.engine.spi.ExecutableList@19e7a160 collectionUpdates=org.hibernate.engine.spi.ExecutableList@662706a7 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@45a4b042 unresolvedInsertDependencies=UnresolvedEntityInsertActions[]]
transaction rollback.
session closed.

(看完/读完)这篇文章有何感想! 999yzc亚洲城在线娱乐城的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注