java hibernate 多对多_java - hibernate多对多问题
映射文件如下:
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();
// 創(chuàng)建對(duì)象
Teacher t1 = new Teacher("韓老師");
Teacher t2 = new Teacher("王老師");
Student s1 = new Student("小明");
Student s2 = new Student("小藍(lán)");
// 建立關(guān)聯(lián)關(guān)系
t1.getStudentSet().add(s1);
t1.getStudentSet().add(s2);
t2.getStudentSet().add(s2);
s1.getTeacherSet().add(t1);
s2.getTeacherSet().add(t1);
s2.getTeacherSet().add(t2);
// 由主動(dòng)方維護(hù)關(guān)聯(lián)關(guān)系 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.");
}
}
執(zhí)行以上方法時(shí)事務(wù)為什么不會(huì)提交?
下面是控制臺(tái)打印的信息
八月 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.
總結(jié)
以上是生活随笔為你收集整理的java hibernate 多对多_java - hibernate多对多问题的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java并发集合面试题,那些经常被问的J
- 下一篇: java定时器 并发_【java多线程与