zxinger
zxinger
2008-11-17 18:09
浏览 192
已采纳

一个登录实例的hibernate 映射问题

这是一个hibernate+struts的登录实验。用的mySQL数据库,struts控制的前台没有问题,只是一登录就出现这种错误。
org.hibernate.hql.ast.QuerySyntaxException: user is not mapped. [FROM user AS u WHERE u.uerName=? AND u.password=?]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:157)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:265)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:218)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:158)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.netBook.impl.userImpl.isUser(userImpl.java:30)
at com.netBook.struts.action.LoginAction.insert(LoginAction.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:274)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:194)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:212)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:818)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:624)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)
Caused by: user is not mapped.
... 45 more
我的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">

<session-factory>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/netbook</property>
    <property name="connection.username">root</property>
    <property name="connection.password">8936</property>
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="myeclipse.connection.profile">
        mySQLDatabase
    </property>
    <mapping resource="com/netBook/pojo/User.hbm.xml" />

</session-factory>


我的pojo类为:
package com.netBook.pojo;

public class User {
private String userName;
private String password;
private String sex;
private String telepone;
private String Email;
private String address;
private String postalcode;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return Email;
}
public void setEmail(String Email) {
this.Email = Email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getTelepone() {
return telepone;
}
public void setTelepone(String telepone) {
this.telepone = telepone;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
我的User.hbm.xml文件为:
<?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">



























session工厂为:
package com.netBook.factory;

import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

public class sessionFactory {
public static Session getSession(){
Session session=null;
session = new Configuration().configure().buildSessionFactory()
.openSession();
return session;
}
}
dao工厂为:
package com.netBook.factory;

import com.netBook.dao.userDao;
import com.netBook.impl.userImpl;

public class daoFactory {
public static userDao getUserImpl(){
return new userImpl();
}
}

dao接口为:
package com.netBook.dao;

import java.util.List;

import com.netBook.pojo.User;

public interface userDao {
// 判断是否会员
public boolean isUser(User user) throws Exception;
}
实现为:
package com.netBook.impl;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import com.netBook.dao.userDao;
import com.netBook.factory.sessionFactory;
import com.netBook.pojo.User;

public class userImpl implements userDao{
private Session session;
public userImpl(){
this.session=sessionFactory.getSession();
}

public boolean isUser(User user) throws Exception {
    boolean flag=false;
    String hql="FROM user AS u WHERE u.uerName=? AND u.password=?";
    Query q=this.session.createQuery(hql);
    q.setString(0, user.getUserName());
    q.setString(1, user.getPassword());
    Iterator iter=q.list().iterator();
    if(iter.hasNext()){
        flag=true;
    }
    this.session.close();
    return flag;
}

}
本人刚接触框架,是在看不出错在哪里?特此请教!只保留登录功能,数据库已经插入数据。如若有必要,将工程上传,请高手过目。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • iteye_521
    iteye_521 2008-11-17 18:12
    已采纳

    [quote]FROM [color=red]user[/color] AS u WHERE u.uerName=? AND u.password=?[/quote]
    这个HQL语句中的user需要跟你的类名一样,改成User,首字母大写才行。

    点赞 评论

相关推荐