HibernateUtil.java
public class HibernateUtil {
 private static SessionFactory sessionFactory;
 static{
 try{ 
 sessionFactory=new Configuration().configure().buildSessionFactory();
 }catch(Throwable ex){
 ex.printStackTrace();
 System.out.println("Initial SessionFactory creation failed.");
 throw new ExceptionInInitializerError(ex);
 }
}
 
 public static SessionFactory getSessionFactory() throws HibernateException{
   return sessionFactory;  
 }}LoginAction.java
public class LoginAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException { // TODO Auto-generated method stub
        System.out.println("Action do1");
HttpSession session = request.getSession();
System.out.println("Action do2");
Session s = HibernateUtil.getSessionFactory().getCurrentSession();
System.out.println("Action do3");
LoginForm loginForm = (LoginForm) form; String sort = loginForm.getSort();
String username = loginForm.getUsername();
String password = loginForm.getPassword();
int loginSort = Integer.parseInt(sort); String[] userlist = new String[2];
userlist[0] = username;
userlist[1] = password; try {
Transaction tx=s.beginTransaction();
String str = new String();
switch (loginSort) {
case 1:
str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword";
Query query = s.createQuery(str);
System.out.println(username + "  " + password);
query.setString("stuName", username);
query.setString("stuPassword", password);
if (query.list().size() > 0) {

s.close();
return mapping.findForward("studentLoginsuccess");
} else
break;
case 2:
str = " from Teacher tea where tea.name = '" + username
+ "' and tea.password ='" + password + "'";
if (s.createQuery(str).list().size() > 0) {

s.close();
return mapping.findForward("teacherLoginsuccess");
} else
break;
case 3:
str = " from Admin admin where admin.name = '" + username
+ "' and admin.password ='" + password + "'";
System.out.println(username + "  " + password);
if (s.createQuery(str).list().size() > 0) {
session.setAttribute("id", ((Admin) s.createQuery(str)
.list().get(0)).getId());
s.close();
return mapping.findForward("adminLoginsuccess");
} else
break;
default:
break;
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
s.close();
} ActionMessages errors = new ActionMessages();
errors.add("login error", new ActionMessage("login.error"));
saveErrors(request, errors);
return mapping.getInputForward(); }
}
上面是我的Action和HibernateUtil文件

解决方案 »

  1.   

    Admin.hbm.xml<?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <!-- 
        Mapping file autogenerated by MyEclipse - Hibernate Tools
    -->
    <hibernate-mapping>
        <class name="com.stuman.domain.Admin" table="admin">
            <id name="id" type="integer">
                <column name="id" />
                <generator class="increment" />
            </id>
            <property name="name" type="string">
                <column name="name" length="32" />
            </property>
            <property name="password" type="string">
                <column name="password" length="32" />
            </property>
        </class>
    </hibernate-mapping>
    hibernate.cfg.xml
    <?xml version='1.0' encoding='utf-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
            "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
            "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration><session-factory> <!-- Database connection settings -->
    <property name="connection.driver_class">
    com.microsoft.sqlserver.jdbc.SQLServerDriver
    </property>
    <property name="connection.url">
    jdbc:sqlserver://192.168.0.248:1433;database=test;
    </property>
    <property name="connection.username">sa</property>
    <property name="connection.password">dzhdba</property> <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property> <!-- SQL dialect -->
    <property name="dialect">
    org.hibernate.dialect.SQLServerDialect
    </property> <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">
    org.hibernate.cache.NoCacheProvider
    </property> <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property> <mapping resource="com/stuman/domain/Admin.hbm.xml" />

    </session-factory></hibernate-configuration>以上是我的hibernate配置文件和管理员对象的映射文件
      

  2.   

    请问没做事务是什么意思...
    是不是说transaction需要commit()下
      

  3.   

    解决了!
    好像是Session   s   =   HibernateUtil.getSessionFactory().getCurrentSession()中getCurrentSession()方法的问题,改成openSession()以后就可以运行了!