java - hibernate多对多问题
PHP中文网
PHP中文网 2017-04-17 14:43:06
0
0
775

映射文件如下:

<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().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 <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 --> 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.

PHP中文网
PHP中文网

认证0级讲师

reply all(0)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template