Hibernate:无法解析属性用户名

发布于 2024-10-19 20:26:37 字数 6112 浏览 2 评论 0 原文

User.hbm.xml

<hibernate-mapping>
<class name="server.dao.domain.User" table="user" catalog="story_v1">
    <id name="userId" type="java.lang.Long">
        <column name="user_id" />
        <generator class="identity" />
    </id>
    <property name="userName" type="string">
        <column name="user_name" length="30" not-null="true" unique="true" />
    </property>
    <property name="password" type="string">
        <column name="password" length="32" not-null="true" />
    </property>
</class>

User.java

public class User implements java.io.Serializable {

private Long userId;
private String userName;
private String password;

public User() {
}

public User(String userName, String password) {
    this.userName = userName;
    this.password = password;
}

public Long getUserId() {
    return this.userId;
}

public void setUserId(Long userId) {
    this.userId = userId;
}

public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

UserHome.java

public class UserHome {

private static final Log log = LogFactory.getLog(UserHome.class);

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
    return HibernateSessionFactory.getSessionFactory();
}

public void createAccount(User entity) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    session.save(entity);

    session.getTransaction().commit();
}

public void deleteAccount(User entity) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    entity = (User) session.get(User.class, entity.getUserId());
    session.delete(entity);

    session.getTransaction().commit();
}

public User findByName(String name) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    Criteria critera = session.createCriteria(User.class);

    critera.add( Restrictions.eq("userName", name) );
    List<User> user = critera.list();

    session.getTransaction().commit();

    return (User) (user.size()==0 ? null : user.get(0) );
}


public User findById(long id) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    User user = (User) session.get(User.class, id);

    session.getTransaction().commit();

    return user;
}

}

在index.jsp中: UserHome 呃 = new UserHome(); 用户 usr = uh.findByName("handy");

错误信息:

org.hibernate.QueryException: could not resolve property: username of: com.nokia.msn.sb.server.dao.domain.User
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:409)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:371)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:316)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:86)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1473)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at com.nokia.msn.sb.server.dao.domain.UserHome.findByName(UserHome.java:59)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

User.hbm.xml

<hibernate-mapping>
<class name="server.dao.domain.User" table="user" catalog="story_v1">
    <id name="userId" type="java.lang.Long">
        <column name="user_id" />
        <generator class="identity" />
    </id>
    <property name="userName" type="string">
        <column name="user_name" length="30" not-null="true" unique="true" />
    </property>
    <property name="password" type="string">
        <column name="password" length="32" not-null="true" />
    </property>
</class>

User.java

public class User implements java.io.Serializable {

private Long userId;
private String userName;
private String password;

public User() {
}

public User(String userName, String password) {
    this.userName = userName;
    this.password = password;
}

public Long getUserId() {
    return this.userId;
}

public void setUserId(Long userId) {
    this.userId = userId;
}

public String getUserName() {
    return this.userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getPassword() {
    return this.password;
}

public void setPassword(String password) {
    this.password = password;
}

}

UserHome.java

public class UserHome {

private static final Log log = LogFactory.getLog(UserHome.class);

private final SessionFactory sessionFactory = getSessionFactory();

protected SessionFactory getSessionFactory() {
    return HibernateSessionFactory.getSessionFactory();
}

public void createAccount(User entity) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    session.save(entity);

    session.getTransaction().commit();
}

public void deleteAccount(User entity) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    entity = (User) session.get(User.class, entity.getUserId());
    session.delete(entity);

    session.getTransaction().commit();
}

public User findByName(String name) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    Criteria critera = session.createCriteria(User.class);

    critera.add( Restrictions.eq("userName", name) );
    List<User> user = critera.list();

    session.getTransaction().commit();

    return (User) (user.size()==0 ? null : user.get(0) );
}


public User findById(long id) {
    Session session = sessionFactory.getCurrentSession();
    session.beginTransaction();

    User user = (User) session.get(User.class, id);

    session.getTransaction().commit();

    return user;
}

}

in index.jsp:
UserHome uh = new UserHome();
User usr = uh.findByName("handy");

Error Message:

org.hibernate.QueryException: could not resolve property: username of: com.nokia.msn.sb.server.dao.domain.User
at org.hibernate.persister.entity.AbstractPropertyMapping.throwPropertyException(AbstractPropertyMapping.java:43)
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:37)
at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1282)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1257)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:409)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumnsUsingProjection(CriteriaQueryTranslator.java:371)
at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:45)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:316)
at org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:86)
at org.hibernate.loader.criteria.CriteriaLoader.<init>(CriteriaLoader.java:67)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1473)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:298)
at com.nokia.msn.sb.server.dao.domain.UserHome.findByName(UserHome.java:59)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:70)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:541)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:383)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:288)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅此而已 2024-10-26 20:26:37

添加

更改 对于
在你的休眠映射中。

add <hibernate-mapping package="com.nokia.msn.sb.server.dao.domain">

change <class name="server.dao.domain.User" table="user" catalog="story_v1"> for <class name="User" table="user" catalog="story_v1">
in your hibernate mapping.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文