我是hibernate的新手。我正在尝试从“用户”表中获取所有用户的列表,我收到以下错误:
org.hibernate.hql.ast.QuerySyntaxException: User is not mapped [from User]
这是我写给用户的代码:
package com.test;
import java.util.Iterator;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class UserLogin extends Action {
private static SessionFactory factory;
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
// User user = new User();
// user.setUserid(request.getParameter("userid"));
// user.setPassword(request.getParameter("password"));
factory = new Configuration().configure().buildSessionFactory();
SessionFactory sf = (SessionFactory) servlet.getServletContext().getAttribute(HibernatePlugin.SESSION_FACTORY_KEY);
System.out.println("SessionFactory" +sf);
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
List<User> users = (List<User>) session.createQuery("from User").list();
// List users = session.createQuery("FROM User").list();
System.out.println("users" +users);
for (Iterator iterator = users.iterator(); iterator.hasNext();){
User user = (User)iterator.next();
System.out.println("First Name: " + user.getUserid());
System.out.println(" Last Name: " + user.getPassword());
}
return mapping.findForward("success");
}
}
这是我的Hibernate配置文件
<?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>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>
<!--<mapping resource="user.hbm.xml"/>-->
</session-factory>
</hibernate-configuration>
这是我的用户类
package com.test;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
*
* @author bhavya.t
*/
@Entity
@Table(name = "login")
public class User implements Serializable {
public User() {}
public User(String userid, String password) {
this.userid = userid;
this.password = password;
}
private String userid;
private String password;
@Id
@Column(name = "password")
public String getPassword() {
return password;
}
/**
* @param password the password to set
*/
public void setPassword(String password) {
this.password = password;
}
/**
* @return the userid
*/
@Id
@Column(name = "userid")
public String getUserid() {
return userid;
}
/**
* @param userid the userid to set
*/
public void setUserid(String userid) {
this.userid = userid;
}
}
这是我的插件类
package com.test;
import java.net.URL;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
*
* @author bhavya.t
*/
public class HibernatePlugin implements PlugIn{
private String _configFilePath = "/hibernate.cfg.xml";
public static final String SESSION_FACTORY_KEY = SessionFactory.class.getName();
private SessionFactory _factory;
public void destroy() {
try{
_factory.close();
}catch(HibernateException e){
System.out.println("Unable to close Hibernate Session Factory: " + e.getMessage());
}
}
public void init(ActionServlet servlet,ModuleConfig config) throws ServletException {
Configuration configuration = null;
URL configFileURL = null;
ServletContext context = null;
try{
configFileURL =
HibernatePlugin.class.getResource(_configFilePath);
context = servlet.getServletContext();
configuration = (new Configuration()).configure(configFileURL);
_factory = configuration.buildSessionFactory();
//Set the factory into session
context.setAttribute(SESSION_FACTORY_KEY, _factory);
}catch(HibernateException e){
System.out.println("Error while initializing hibernate: " + e.getMessage());
}
}
public void setConfigFilePath(String configFilePath) {
if ((configFilePath == null) || (configFilePath.trim().length() == 0)) {
throw new IllegalArgumentException("configFilePath cannot be blank or null.");
}
System.out.println("Setting ' configFilePath' to '" + configFilePath + "'...");
_configFilePath = configFilePath;
}
}
这是Hbm文件..
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.test">
<class dynamic-insert="false" dynamic-update="false" mutable="true" name="User" optimistic-lock="version" polymorphism="implicit" select-before-update="false" table="login">
<meta attribute="class-description">
This class contains the employee detail.
</meta>
<!-- <id name="id" type="int" column="id">
<generator class="native"/>
</id>-->
<property name="userid" column="userid" type="string"/>
<property name="password" column="password" type="string"/>
</class>
</hibernate-mapping>
请帮助我任何人
答案 0 :(得分:0)
您正在为hibernate混合注释配置和xml配置。首先,用<mapping resource="user.hbm.xml"/>
替换<mapping class="com.test.User"/>
(现在Hibernate只会查看您的注释并忽略映射xml文件)。其次,您将userid
和password
都映射为@Id
。您应该为您的用户拥有自动生成的ID,或者使用他们的用户名作为ID。如果是后者,请从@Id
移除getPassword()
,然后重试。可能还有一些问题,所以在评论中写下您的进度和新问题(如果有的话),如果可以的话,我会帮助您。