求救???org.hibernate.TransactionException: Transaction not successfully started

iefus 2010-09-25 10:33:29
在学习Hibernate
有个car的数据库,只有一个表basiccar
四个字段:
id,name,factory,date
我编写了POJO文件,也映射了,也写了Hibernate配置文件
POJO:

package basicCar.bean;

import java.util.Date;

public class BasicCar implements java.io.Serializable{
private long id;
private String name;
private String factory;
private Date date;
public BasicCar(){

}
public BasicCar(String name,String factory,Date date){
this.name = name;
this.factory = factory;
this.date = date;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFactory() {
return factory;
}
public void setFactory(String factory) {
this.factory = factory;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}

}



映射文件:

<hibernate-mapping>

<class name="basicCar.bean.BasicCar" table="basiccar">

<id
name="id"
type="long"
column="id"
>
<generator class="increment" />
</id>

<property name="name" type="java.lang.String" column="name" />
<property name="factory" type="java.lang.String" column="factory" />
<property name="date" type="date" column="date" />


</class>
</hibernate-mapping>

Hibernate;

<hibernate-configuration>

<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.username">root</property>
<property name="connection.password">1119</property>
<property name="connection.url">jdbc:mysql://localhost:3306/car</property>
<property name="show_sql">true</property>

<mapping resource="basicCar/bean/BasicCar.hbm.xml" />

</session-factory>
</hibernate-configuration>


util:

package com.chuanjie56.util;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class HibernateSessionFactory {

private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";

@SuppressWarnings("unchecked")
private static final ThreadLocal threadLocal = new ThreadLocal();

private static final Configuration cfg = new Configuration();

private static org.hibernate.SessionFactory sessionFactory;

@SuppressWarnings("unchecked")
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}

return session;
}


@SuppressWarnings("unchecked")
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);

if (session != null) {
session.close();
}
}

private HibernateSessionFactory() {
}

}


测试文件;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Test test = new Test();
//test.saveEntity();
test.updateEntiry();
//test.deleteEntity();
}

void saveEntity(){
Session session=null;
Transaction tx = null;
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
BasicCar bc = new BasicCar();
bc.setFactory("Beijing123");
bc.setName("BJcar123");
bc.setDate(Date.valueOf("1966-02-03"));
session.save(bc);
tx.commit();
}
void deleteEntity(){
Session session=null;
Transaction tx = null;
try{
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
BasicCar bc = (BasicCar)session.load(BasicCar.class, new Long(1));
session.delete(bc);
tx.commit();
}catch(HibernateException e){
throw e;
}finally{
if(tx!=null)
tx.rollback();
HibernateSessionFactory.closeSession();
}
}
void updateEntiry(){
Session session=null;
Transaction tx = null;
try{
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
BasicCar bc = (BasicCar)session.load(BasicCar.class, new Long(2));
bc.setName("shCar");
bc.setFactory("SH00");
session.update(bc);
tx.commit();
}catch(HibernateException e){
throw e;
}finally{
if(tx!=null)
tx.rollback();
HibernateSessionFactory.closeSession();
}
}
void queryEntity(){
Session session=null;
try{
session = HibernateSessionFactory.currentSession();
Query query = session.createQuery("from BasicCar");
BasicCar bc = (BasicCar)query.list().get(0);
System.out.println("query:"+bc.getName());
}catch(Exception e){

}
}
}


增加,查询操作没问题,
但是删除和更新操作报异常:
Exception in thread "main" org.hibernate.TransactionException: Transaction not successfully started
at org.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:183)
at basicCar.Test.updateEntiry(Test.java:76)
at basicCar.Test.main(Test.java:27)


不知道为什么


...全文
280 点赞 收藏 9
写回复
9 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
顶楼上,我也看的这本书,11年版的还是有错误,还高手真经呢!!!
回复
taishan07 2011-11-20
我也犯了这个错误,平时不是这样写的,这次跟着书写,错了;呵呵呵;
《java高手真经java web和性框架》2009年3月版里错的,
回复
lylovejava3 2010-10-20
笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋笨蛋v
回复
youjianbo_han_87 2010-09-30
恭喜问题已经解决了,注意程序逻辑。
回复
liufeng0209 2010-09-30
结帖散分啊
回复
iefus 2010-09-29
这样阿,谢谢各位
回复
aoli444 2010-09-29
tx.commit(); 后在执行tx.rollback()是没有效果的。
回复
iefus 2010-09-29
情况是楼上说的那样,放在catch里面就不报错了

但是为什么我执行更新操作时,若
if(tx!=null)
tx.rollback()
放在finally里面,tx执行了rollback()

数据库里面还是跟新了呢,不应该回滚么
回复
liufeng0209 2010-09-26
if(tx!=null)
tx.rollback();
放在catch块里,别放在finlly块里,这样每次必然执行,不出错才怪了
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2010-09-25 10:33
社区公告
暂无公告