一直出现Transaction not successfully started  请问这是什么原因啊????
package com.framework.beans;import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
public class User {

private int id;
private String username;
private String password;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}

}<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
          "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.framework.beans">
<class name="User" table="tb_user">
<id name="id" column="ID" type="integer">
<generator class="native"/>
</id>
<property name="username" column="username" type="string"/>
<property name="password" column="password" type="string"/>
<property name="email" column="email" type="string"/>

</class></hibernate-mapping>
package com.framework.dao;import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;import com.framework.beans.User;
import com.framework.util.HibernateSessionFactory;
public class UserDAO {


public User getUser(String username) throws HibernateException{
Session session = null;
Transaction tx = null;
User user=null;
try {
session = HibernateSessionFactory.currentSession();
tx=session.beginTransaction();
Query query = session.createQuery("from User where username= ? ");
query.setString(0, username.trim());
user = (User) query.uniqueResult();
query=null;

tx.commit();
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("UserDAO");
throw e;

}finally{
if (tx!=null) {
tx.rollback();
}
session.close();
}
return user;
}
}package com.framework.service;import com.framework.beans.User;
import com.framework.dao.UserDAO;public class UserService {

public boolean valid(String username,String password){
UserDAO userDAO = new UserDAO();
User user= userDAO.getUser("admin");
if(user.getPassword().equals(password)){
return true;
}else {
return false;
}
}


public static void main(String[] args){
UserService service = new UserService();
boolean login=service.valid("admin", "admin");
System.out.println("boolean="+login);
}
}
public class HibernateSessionFactory {    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal threadLocal = new ThreadLocal();
    private  static Configuration configuration = new Configuration();    
    private static org.hibernate.SessionFactory sessionFactory;
    private static String configFile = CONFIG_FILE_LOCATION;
    
    public static Session currentSession() throws HibernateException{
     Session session = (Session)threadLocal.get();
     if(session==null){
     if(sessionFactory==null){
     try {
     configuration.configure(configFile);
         sessionFactory = configuration.buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
System.out.println("session 获取失败");
}
     }
     session = sessionFactory.openSession();
     threadLocal.set(session);
     }
     return session;
    }
    
    public static void closeSession() throws HibernateException{
     Session session = (Session) threadLocal.get();
     threadLocal.set(null);
     if(session!=null){
     session.close();
     }
    }
}一直出现Transaction not successfully started  请问这是什么原因啊????

解决方案 »

  1.   

       Transaction not successfully started 事务有问题的话就只能下面的代码有问题了 session = HibernateSessionFactory.currentSession();
                tx=session.beginTransaction();这个session是对的吗?HibernateSessionFactory这个工具类的代码你没有展示出来啊!
      

  2.   

    HibernateSessionFactory.currentSession();换成
    HibernateSessionFactory.openSession();
    HibernateSessionFactory我估计是myeclipse自动生成的那个吧?
      

  3.   

    不好意思 ,代码贴多了  忘了贴上来public class HibernateSessionFactory {    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();    
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
        
        public static Session currentSession() throws HibernateException{
         Session session = (Session)threadLocal.get();
         if(session==null){
         if(sessionFactory==null){
         try {
         configuration.configure(configFile);
             sessionFactory = configuration.buildSessionFactory();
    } catch (HibernateException e) {
    e.printStackTrace();
    System.out.println("session 获取失败");
    }
         }
         session = sessionFactory.openSession();
         threadLocal.set(session);
         }
         return session;
        }
        
        public static void closeSession() throws HibernateException{
         Session session = (Session) threadLocal.get();
         threadLocal.set(null);
         if(session!=null){
         session.close();
         }
        }
    }
      

  4.   

    不好意思 ,代码贴多了  忘了贴上来public class HibernateSessionFactory {    private static String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
    private static final ThreadLocal threadLocal = new ThreadLocal();
        private  static Configuration configuration = new Configuration();    
        private static org.hibernate.SessionFactory sessionFactory;
        private static String configFile = CONFIG_FILE_LOCATION;
        
        public static Session currentSession() throws HibernateException{
         Session session = (Session)threadLocal.get();
         if(session==null){
         if(sessionFactory==null){
         try {
         configuration.configure(configFile);
             sessionFactory = configuration.buildSessionFactory();
    } catch (HibernateException e) {
    e.printStackTrace();
    System.out.println("session 获取失败");
    }
         }
         session = sessionFactory.openSession();
         threadLocal.set(session);
         }
         return session;
        }
        
        public static void closeSession() throws HibernateException{
         Session session = (Session) threadLocal.get();
         threadLocal.set(null);
         if(session!=null){
         session.close();
         }
        }
    }