使用注释映射hibernate调用时出错

时间:2011-10-17 03:50:05

标签: hibernate spring-mvc

我得到了这个ewrror。

Oct 15, 2011 6:30:11 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/AdministrativeApplication] threw exception [Request processing failed; nested exception is org.hibernate.hql.ast.QuerySyntaxException: tbluser is not mapped [FROM  tbluser]] with root cause
org.hibernate.hql.ast.QuerySyntaxException: tbluser is not mapped [FROM  tbluser]
    at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)

我使用hibernate注释类的方法如下。

package test.user;

import java.io.Serializable;
import java.sql.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
import javax.persistence.GenerationType;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * For a complete reference see
 * <a href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/">
 * Hibernate Annotations Communit Documentations</a>
 */
@Entity
@Table(name = "tbluser")
public class User implements Serializable{


    @Column
private String password;
    @Column
private String firstName;
    @Column
private String middleName;
    @Column
private String lastName;
    @Column
    private String emailAddress;
    @Column
    private Date lastLoginDate;

/**
     * @return the emailAddress
     */
    public String getEmailAddress() {
        return emailAddress;
    }
    /**
     * @param emailAddress the emailAddress to set
     */
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    /**
     * @return the lastLoginDate
     */
    public Date getLastLoginDate() {
        return lastLoginDate;
    }
    /**
     * @param lastLoginDate the lastLoginDate to set
     */
    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }
@Id
@NotEmpty
@Column(name="userId")
private String userId;






import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.swamahiman.apps.user.User;

/**
 * Service for processing Users
 *
 */
@Service("userService")
@Transactional
public class UserService {

 protected static Logger logger = Logger.getLogger("service");

 @Resource(name="sessionFactory")
 private SessionFactory sessionFactory;

 /**
  * Retrieves all Users
  *
  * @return a list of Users
  */
 public List<User> getAll() {
  logger.debug("Retrieving all Users");

  // Retrieve session from Hibernate
  Session session = sessionFactory.getCurrentSession();

  // Create a Hibernate query (HQL)
  Query query = session.createQuery("FROM  tbluser");

  // Retrieve all
  return  query.list();
 }

 /**
  * Retrieves a single User
  */
 public User get( Integer id ) {
  // Retrieve session from Hibernate
  Session session = sessionFactory.getCurrentSession();

  // Retrieve existing User first
  User User = (User) session.get(User.class, id);

  return User;
 }
 /**
  * Adds a new User
  */
 public void add(User User) {
  logger.debug("Adding new User");

  // Retrieve session from Hibernate
  Session session = sessionFactory.getCurrentSession();

  // Save
  session.save(User);
 }

 /**
  * Deletes an existing User
  * @param id the id of the existing User
  */
 public void delete(Integer id) {
  logger.debug("Deleting existing User");

  // Retrieve session from Hibernate
  Session session = sessionFactory.getCurrentSession();

  // Retrieve existing User first
  User User = (User) session.get(User.class, id);

  // Delete
  session.delete(User);
 }

 /**
  * Edits an existing User
  */
 public void edit(User User) {
  logger.debug("Editing existing User");

  // Retrieve session from Hibernate
  Session session = sessionFactory.getCurrentSession();

  // Retrieve existing User via id
  User existingUser = (User) session.get(User.class, User.getUserId());

  // Assign updated values to this User
  existingUser.setFirstName(User.getFirstName());
  existingUser.setLastName(existingUser.getLastName());
  existingUser.setPassword(existingUser.getPassword());

  // Save updates
  session.save(existingUser);
 }
}

这就是我调用hibernate调用的方式。

        @RequestMapping(value="/home" , method = RequestMethod.GET)
    //@ModelAttribute("user")
        public String process(@ModelAttribute("user") User user,
BindingResult result, Model model){
       // public String get(final ModelMap model) {
            System.out.println("inside here get returning model ..");
             List<User> users = userService.getAll();
              User userObj= null;
             // Attach users to the Model
             //model.addAttribute("users", users);
            Iterator<User> iterate= users.iterator();
            while(iterate.hasNext()) {
                userObj=iterate.next();
                if (userObj.getUserId().equals("sjoe")){
                    break;
                }

            }

      model.addAttribute("user", userObj);
   return "home";
        }

我的tbluser表有。

的firstName 姓 中间名字 lastLoginUpdate 密码 userId字段。

由于

Dhiren

1 个答案:

答案 0 :(得分:3)

这是无效的HQL,在HQL中使用实体名称,而不是数据库表的名称。

Query query = session.createQuery("FROM  tbluser");

请改为尝试:

Query query = session.createQuery("FROM User"); 

可以从以下网址找到更多信息:documentation