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(
来源:学生作业帮助网 编辑:六六作业网 时间:2024/12/18 16:34:14
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(
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)
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(
tx=session.getTransaction();
改成
tx=session.beginTransaction();
异常信息说了事务没有成功的开始.