一直出现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 请问这是什么原因啊????
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 请问这是什么原因啊????
解决方案 »
- 敲代码 不求大神指点 只求入门
- 为什么要用final
- MyEclipse移除xfire
- 文件类容怎样才会引起404错误啊 急啊,在线等
- myeclipse部署web工程的困惑
- VBScript 运行时错误 '800a01fa'
- 请教cobol学习?
- 用客户端测试sessionbean,总出现下面的错误.........................
- 使用struts时的问题
- 我都糊涂了:Servlet、JSP、JavaBean、EJB还有JavaScript他们什么关系啊?
- 菜鸟学习struts受阻,求救大神,启动tomcat时报错,求解决,感激不尽!
- YUI Compressor 压缩大的js文件出现问题,请高手指点!
tx=session.beginTransaction();这个session是对的吗?HibernateSessionFactory这个工具类的代码你没有展示出来啊!
HibernateSessionFactory.openSession();
HibernateSessionFactory我估计是myeclipse自动生成的那个吧?
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();
}
}
}
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();
}
}
}