hibernate问题:Transaction not successfully started

hibernate问题:Transaction not successfully started
测试类:import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class Test {
x05public static void main(String[] args) {
x05x05
x05x05Configuration conf=new Configuration().configure();
x05x05SessionFactory sf=conf.buildSessionFactory();
x05x05Session session=sf.openSession();
x05x05Transaction tx=null;
x05x05try{
x05x05x05tx=session.getTransaction();
x05x05x05User user=new User();
x05x05x05user.setUname("username");
x05x05x05user.setUpass("password");
x05x05x05session.save(user);
x05x05x05tx.commit();
x05x05}catch(Exception e){
x05x05x05if(null!=tx){
x05x05x05x05tx.rollback();
x05x05x05}
x05x05x05e.printStackTrace();
x05x05}finally{
x05x05x05session.close();
x05x05}
x05}
}
报错信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Exception in thread "main" org.hibernate.TransactionException:Transaction not successfully started
x05at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:149)
x05at cn.hibernate.entity.Test.main(Test.java:32)
七月qjj 1年前 已收到1个回答 举报

baohandsome 幼苗

共回答了16个问题采纳率:87.5% 举报

tx=session.getTransaction();
改成
tx=session.beginTransaction();
异常信息说了事务没有成功的开始.

1年前 追问

6

七月qjj 举报

呵呵,是这步写错了,找了半天没找到错,谢谢了
可能相似的问题
Copyright © 2024 YULUCN.COM - 雨露学习互助 - 18 q. 0.094 s. - webmaster@yulucn.com