日期:2014-05-18  浏览次数:20649 次

有趣的struts+hibernate的问题
public   class   Logon   {

public   boolean   validate(HelloForm   hf)   {

SessionFactory   sf   =   new   Configuration().configure().buildSessionFactory();
Session   session   =   sf.openSession();
//private   Transaction   tran   =   session.beginTransaction();
//private   Myusertable   user   =   new   Myusertable();
//HelloForm   hf;
Myusertable   user   =   new   Myusertable();

boolean   isUser   =   false;

try   {

Query   query   =   session.createQuery( "from   Myusertable   as   user   where   user.UserName= ' "   +   hf.getUserName()   + " '   and   user.Password= ' "   +   hf.getPassword()   + " ' ");

if   (!query.list().isEmpty())   {

isUser   =   true;
}
}   catch   (HibernateException   e)   {
e.printStackTrace();
}

return   isUser;
}

public   static   void   main(String   []   args)   {
SessionFactory   sf   =   new   Configuration().configure().buildSessionFactory();
Session   session   =   sf.openSession();
//private   Transaction   tran   =   session.beginTransaction();
Myusertable   user   =   new   Myusertable();

HelloForm   hf   =   new   HelloForm();
hf.setUserName( "qbq ");
hf.setPassword( "1 ");

boolean   isUser   =   false;

try   {

Query   query   =   session.createQuery( "from   Myusertable   as   user   where   user.UserName= ' "   +   hf.getUserName()   + " '   and   user.Password= ' "   +   hf.getPassword()   + " ' ");

if   (!query.list().isEmpty())   {
isUser   =   true;
}
}   catch   (HibernateException   e)   {
e.printStackTrace();
}
System.out.println(isUser);
}
}

当我把这个类当作一个application运行时是正确的,输出是:
log4j:WARN   No   appenders   could   be   found   for   logger   (org.hibernate.cfg.Environment).
log4j:WARN   Please   initialize   the   log4j   system   properly.
Hibernate:   select   myusertabl0_.ID   as   ID0_,   myusertabl0_.UserName   as   UserName0_,   myusertabl0_.Password   as   Password0_,   myusertabl0_.Email   as   Email0_   from   myusertable   myusertabl0_   where   myusertabl0_.UserName= 'qbq '   and   myusertabl0_.Password= '1 '
true



------解决方案--------------------
你把JDBC的驱动放进你项目的lib包里了吗?