transactions-hibernate多对多问题,求解决!

问题描述

hibernate多对多问题,求解决!

映射文件如下:

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

解决方案

你用的注解吧,,

解决办法是通过在Hibernate配置文件中添加自动提交的

< property name="connection.autocommit">true

再调用的方法上面加@Transactional

解决方案二:

一般把多对多转换成多对一或者一对多

时间: 2024-11-01 08:48:22

transactions-hibernate多对多问题,求解决!的相关文章

org.hibernate.QueryException: could not resolve property: title of:求解决hql

问题描述 org.hibernate.QueryException: could not resolve property: title of:求解决hql org.hibernate.QueryException: could not resolve property: title of: com.head.oa.affairs.archivemanage.entity.ArchiveInfo [select count(*) from com.head.oa.affairs.archivem

hibernate 多对一得跑起来很慢,大家如何解决?

问题描述 hibernate多对一得跑起来很慢,大家如何解决? 解决方案 解决方案二:看你是不是需要及时获得信息.如果要的话fetch='join'看你用不用延迟加载了!!如果用lazy='true',那配fetch='join'作用也不大,就要用select其次就怕关联了spring,这个有优化sql功能.慢慢研究.解决方案三:我比较好奇,楼主是怎么感觉到他慢的?解决方案四:详细点楼主没说清楚尽量懒加载解决方案五:延迟加载,还有就是别用关系

Hibernate多对多关系查询出所有结果

问题描述 Hibernate多对多关系查询出所有结果 问题背景: 现在有三张表,学生.班级.课程,学生和班级为多对多,学生和课程为多对多,班级和课程是多对一,即一门课可以有多个班级. 我写的学生实体类: private Integer id; private String name; private Set<Clazz> clazzs=new HashSet<>(); //n-n private Set<Course> courses=new HashSet<&g

java-初学Hibernate遇到问题,求解答~

问题描述 初学Hibernate遇到问题,求解答~ 运行一个hibernate例子,但是报错,小白不知道该怎么解决,麻烦大神帮看看.我把代码贴出来~(1).package hibernate;import org.hibernate.*;import org.hibernate.cfg.*;import org.hibernate.service.*;import org.hibernate.boot.registry.*;public class NewsManager{ public sta

编码-hibernate运行总是出错 求解答

问题描述 hibernate运行总是出错 求解答 一直出错困扰我两天了 求大家帮忙解决一下 谢谢了一月 18 2016 11:10:26 下午 org.hibernate.Version logVersionINFO: HHH000412: Hibernate Core {5.0.7.Final}一月 18 2016 11:10:26 下午 org.hibernate.cfg.Environment INFO: HHH000206: hibernate.properties not found一

hibernate取值问题 求大神帮助

问题描述 hibernate取值问题 求大神帮助 hibernate 中从一行中取某一列的值,要怎么解决?求大神帮助 解决方案 http://www.ablanxue.com/prone_3552_1.html 解决方案二: 这说的也太模糊了吧!!!!

java-用HQLhelper方式,然后报错,求解决。

问题描述 用HQLhelper方式,然后报错,求解决. 因为在hql语句中2个占位符几个.我查了百度,query.setparameter就可以解决.可是我写的是HQLhelper- 解决方案 ClickOnce 部署中报错的解决方式squid日志报错信息的解决方式启动服务器报错,求解决

Android初学者,求解决这个URI解析

问题描述 Android初学者,求解决这个URI解析 打印出来的信息是读取的同一张图片,选择图库里的就可以获取到,选择其他的就报null 解决方案 http://blog.csdn.net/ljz2009y/article/details/7678027

sql-SQL语句求思路求解决,要晓得部门经理叫什么名字

问题描述 SQL语句求思路求解决,要晓得部门经理叫什么名字 这样说吧,要晓得部门名称 也要晓得部门经理叫什么名字 这个部门在1月份来了多少人, 走了多少人,有多少人派遣出去 这种sql语句如何实现? 求各位大神给点sql思路,小弟不才,想不出来~~~ 解决方案 该问题已自行解决.谢谢大家. 解决方案二: 表结构上怎样的 解决方案三: 题目是怎样的啊,只有问题,没有条件怎么写啊 解决方案四: 这个问题,我解决了...谢谢热忱的道友们! 解决方案五: 确实不好回答,没有表结构,不知道相关字段