java - hibernate多对多问题

 中国传媒大学一零播本更_822 发布于 2022-11-01 07:51

映射文件如下:


  
      
        
          sequence_stuid
        
      
      
      
      
        
        
      
  


  
    
      
        sequence_teaid
      
    
    
    
    
      
      
    
  



@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().applySettings(cfg.getProperties()).build();
      sf = cfg.buildSessionFactory(sr);
      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().add(s1);
      t1.getStudentSet().add(s2);
      t2.getStudentSet().add(s2);

      s1.getTeacherSet().add(t1);
      s2.getTeacherSet().add(t1);
      s2.getTeacherSet().add(t2);
      
      // 由主动方维护关联关系 inverse="false"
      session.save(s1);
      session.save(s2);
      
      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
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
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
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
INFO: HHH000397: Using ASTQueryTranslatorFactory
SessionFactory --> org.hibernate.internal.SessionFactoryImpl@52815fa3
SESSION --> SessionImpl(PersistenceContext[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.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有