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 uh = new UserHome(); 用户usr = uh.findByName(“方便”);
错误讯息:
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)
答案 0 :(得分:0)
添加<hibernate-mapping package="com.nokia.msn.sb.server.dao.domain">
更改<class name="server.dao.domain.User" table="user" catalog="story_v1"> for <class name="User" table="user" catalog="story_v1">
在你的hibernate映射中。