在logonaction.jsp中创建事务创建对话
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LogonForm logonForm = (LogonForm) form;// TODO Auto-generated method stub
//获取提交的用户名和密码
String userName=request.getParameter("userName");
String Password=request.getParameter("Password");
String mPassword=null;
Userlist ul=null;
//创建连接
Session session = HibernateSessionFactory.getSession();(此处报错提示为cannot convert session to session)
//创建事务
Transaction tx=session.beginTransaction();(这里也报错,原因和上面得一样)
//创建对话
Query query=session.createQuery("select u from Usrelist as u where usename='" + userName + "'");
try{
Iterator it=query.iterate();
ul=(Userlist)it.next();
mPassword=ul.getPassword();
}catch(Exception e){
System.out.println(e.getMessage());
}
//事务提交
tx.commit();
//关闭连接
HibernateSessionFactory.closeSession();
if(Password.equals(mPassword))
return(mapping.findForward("gomainMenu"));
else
return(mapping.findForward("rLongon"));}}
按提示修改后不再出错,但变为了
String mPassword=null;
Userlist ul=null;
//创建连接
Session session = (Session) HibernateSessionFactory.getSession();(高手请指点下(Session)什么意思,为什么解决了上面的问题。)
//创建事务
Transaction tx=(Transaction) session.beginTransaction();
同时下面的tx.commit出错,加了一组try catch不再报错。
小弟先谢过各位高手!
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
LogonForm logonForm = (LogonForm) form;// TODO Auto-generated method stub
//获取提交的用户名和密码
String userName=request.getParameter("userName");
String Password=request.getParameter("Password");
String mPassword=null;
Userlist ul=null;
//创建连接
Session session = HibernateSessionFactory.getSession();(此处报错提示为cannot convert session to session)
//创建事务
Transaction tx=session.beginTransaction();(这里也报错,原因和上面得一样)
//创建对话
Query query=session.createQuery("select u from Usrelist as u where usename='" + userName + "'");
try{
Iterator it=query.iterate();
ul=(Userlist)it.next();
mPassword=ul.getPassword();
}catch(Exception e){
System.out.println(e.getMessage());
}
//事务提交
tx.commit();
//关闭连接
HibernateSessionFactory.closeSession();
if(Password.equals(mPassword))
return(mapping.findForward("gomainMenu"));
else
return(mapping.findForward("rLongon"));}}
按提示修改后不再出错,但变为了
String mPassword=null;
Userlist ul=null;
//创建连接
Session session = (Session) HibernateSessionFactory.getSession();(高手请指点下(Session)什么意思,为什么解决了上面的问题。)
//创建事务
Transaction tx=(Transaction) session.beginTransaction();
同时下面的tx.commit出错,加了一组try catch不再报错。
小弟先谢过各位高手!
估计引用的包有冲突HibernateSessionFactory这个类是不是你自定义的?没见过。还有建议不要把业务写在action里。
你看你的import中导入的是哪个Session
package com;import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;/**
* Configures and provides access to Hibernate sessions, tied to the
* current thread of execution. Follows the Thread Local Session
* pattern, see {@link http://hibernate.org/42.html }.
*/
public class HibernateSessionFactory {(session方法调用的不是这个类里的吗?新手问题可能傻点呵呵这个事自动生成的哦。) /**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is
* in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session.
*/
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; static {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private HibernateSessionFactory() {
}
/**
* Returns the ThreadLocal Session instance. Lazy initialize
* the <code>SessionFactory</code> if needed.
*
* @return Session
* @throws HibernateException
*/
public static Session getSession() throws HibernateException {(用的是这里的session吧,因为里面有是否启动的判断应该就不用再设置是否opensession了吧?)
Session session = (Session) threadLocal.get(); if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession()
: null;
threadLocal.set(session);
} return session;
} /**
* Rebuild hibernate session factory
*
*/
public static void rebuildSessionFactory() {
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err
.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
} /**
* Close the single hibernate session instance.
*
* @throws HibernateException
*/
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null); if (session != null) {
session.close();
}
} /**
* return session factory
*
*/
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} /**
* return session factory
*
* session factory will be rebuilded in the next call
*/
public static void setConfigFile(String configFile) {
HibernateSessionFactory.configFile = configFile;
sessionFactory = null;
} /**
* return hibernate configuration
*
*/
public static Configuration getConfiguration() {
return configuration;
}
public static org.hibernate.SessionFactory getSessionFactory() {
return sessionFactory;
} 这个里面写些代码是吗?session调用的是这个里的,不是我认为的那个。感谢指教
Session session = (Session) HibernateSessionFactory.getSession();(高手请指点下(Session)什么意思,为什么解决了上面的问题。)
---------------------------------
它已经是Session的了.为什么还要转一次?
-*---------什么叫这个提示修改的?不是很明白.是不是这句错误啊
-->
Session session = HibernateSessionFactory.getSession();
---------------------------- 因为下面的本来就是Session对象,不要改啊.
public static Session getSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) {
if (sessionFactory == null) {
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
}
return session;
}
Session session = HibernateUtil.getSession();
HibernateUtil是个工具类:public class HibernateUtil {
private static SessionFactory factory;
static {
try{
Configuration cfg=new Configuration().configure();
factory=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return factory;
}
public static Session getSession(){
return factory.openSession();
}
public static void closeSeesion(Session session){
if(session!=null){
if(session.isOpen()){
session.close();
}
}
}
}
希望你能记下来