2011-10-17 88 views
0

我收到了這個錯誤。使用註釋映射hibernate調用時出錯

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 lastName的 中間名 lastLoginUpdate 密碼 用戶id字段。

感謝

Dhiren

回答

3

這不是合法的HQL,HQL中使用實體的名稱,數據庫表中沒有名稱。

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

試試這個:

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

更多信息,可以發現從:documentation

+0

ThanksThat工作 – djoshi