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) {
\x05\x05
\x05\x05Configuration conf=new Configuration().configure();
\x05\x05SessionFactory sf=conf.buildSessionFactory();
\x05\x05Session session=sf.openSession();
\x05\x05Transaction tx=null;
\x05\x05try{
\x05\x05\x05tx=session.getTransaction();
\x05\x05\x05User user=new User();
\x05\x05\x05user.setUname("username");
\x05\x05\x05user.setUpass("password");
\x05\x05\x05session.save(user);
\x05\x05\x05tx.commit();
\x05\x05}catch(Exception e){
\x05\x05\x05if(null!=tx){
\x05\x05\x05\x05tx.rollback();
\x05\x05\x05}
\x05\x05\x05e.printStackTrace();
\x05\x05}finally{
\x05\x05\x05session.close();
\x05\x05}
\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)
1个回答 分类:综合 2014-11-12

问题解答:

我来补答
tx=session.getTransaction();
改成
tx=session.beginTransaction();
异常信息说了事务没有成功的开始.
再问: 呵呵,是这步写错了,找了半天没找到错,谢谢了
 
 
展开全文阅读
剩余:2000
也许感兴趣的知识