刘芳
2015-03-30 08:28
采纳率: 100%
浏览 4.0k
已采纳

hibernate事务开启不成功的错误

package cn.jbit.test;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

import cn.jbit.entity.User;
import cn.jbit.util.HibernateUtil;

public class test {
private static final SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

public static void main(String[] args) {
    Session session=null;
    Transaction tx=null;
    try {       
    Configuration cfg=new Configuration().configure();
    ServiceRegistry serviceRegistry=new ServiceRegistryBuilder().applySettings(cfg.getProperties()).buildServiceRegistry();
    SessionFactory sessionFactory=cfg.buildSessionFactory(serviceRegistry);
    session=sessionFactory.openSession();       
    tx=session.beginTransaction();
    User user=new User();
    user.setName("liufang");        
    user.setBorn(sdf.parse("1990-08-06"));
    session.save(user);
    tx.commit();

    } catch (ParseException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (Exception e) {
        // TODO: handle exception
        if(session!=null){
            session.getTransaction().rollback();
        }
    }finally{

        HibernateUtil.closeSession(session);
    }


}

}

报错信息:
log4j:WARN No appenders could be found for logger (org.jboss.logging).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.rollback(AbstractTransactionImpl.java:200)
at cn.jbit.test.test.main(test.java:43)

相应jar包如图所示:
图片说明

hibernate.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">



org.hibernate.dialect.MySQLDialect
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/test
root

root

true

<!--update
-->


User.hbm.xml配置文件
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<class name="User" table="t_user">
    <id name="id" column="id">
        <generator class="native"><!--
            <param name="table_name">ID_SEQ_TBL_BSC_SEQ</param>                
        --></generator>
    </id>
    <property name="name" type="string"/>
    <property name="born" type="timestamp"/>
</class>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 大河行脚 2015-03-30 08:42
    已采纳

    session.save(user);
    tx.commit();
    //我在提交时是这样写的:tx.getTransaction().commit();

        还有看提示好像你的日志配置的也有问题,你自己检查一下;
        如果回答对您有帮助,请采纳
    
    点赞 打赏 评论
  • 大河行脚 2015-03-30 08:45

    写错了,应该是session.getTransaction().commit();
    你的tx=session.beginTransaction();

    如果回答对您有帮助,请采纳

    点赞 打赏 评论
  • IT天道勤酬 2015-03-30 09:06

    session.getTransaction().commit();

    点赞 打赏 评论

相关推荐 更多相似问题