2014-05-15 47 views
0

我面對exeception爲:org.hibernate.hql.ast.QuerySyntaxException:表中未映射

org.hibernate.hql.ast.QuerySyntaxException: user is not mapped [select s.SubscriptionsId,s.SubscriptionName, s.ChamberOfCommerce, s.AccountingFirmId,(select SubscriptionName from subscriptions where SubscriptionsId=s.AccountingFirmId) as AFName,(select ChamberOfCommerce from subscriptions where SubscriptionsId=s.AccountingFirmId) as AFChamberOfCommerce,(select FtpFolder from subscriptions where SubscriptionsId=s.AccountingFirmId) as FtpFolder from user u,subscriptions s, usersubscriptions us where u.openID=:openId and u.UserId=us.UserId and us.SubscriptionsId = s.SubscriptionsId] 

我的代碼如下:

Query query = this.getSession().createQuery(
    this.getQuery("com.yob.sbs.dpc.getSubsctionByOpenId")) 
     .setParameter("openId", openId); 

我使用Hibernate查詢與關鍵com.yob.sbs.dpc.getSubsctionByOpenId。而我的查詢如下:

com.yob.sbs.dpc.getSubsctionByOpenId = 
select 
    s.SubscriptionsId, 
    s.SubscriptionName, 
    s.ChamberOfCommerce, 
    s.AccountingFirmId, 
    (select SubscriptionName from subscriptions where 
     SubscriptionsId = s.AccountingFirmId) as AFName, 
    (select ChamberOfCommerce from subscriptions where 
     SubscriptionsId = s.AccountingFirmId) as AFChamberOfCommerce, 
    (select FtpFolder from subscriptions where 
     SubscriptionsId = s.AccountingFirmId) as FtpFolder 
from user u, subscriptions s, usersubscriptions us 
where 
    u.openID = :openId and 
    u.UserId = us.UserId and 
    us.SubscriptionsId = s.SubscriptionsId 

和用戶數據對象如下:

package com.yob.dps.dataobjects; 

import java.io.Serializable; 
import java.lang.reflect.InvocationTargetException; 
import java.util.Date; 
import java.util.HashSet; 
import java.util.Map; 
import java.util.Set; 

import javax.persistence.CascadeType; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.FetchType; 
import javax.persistence.GeneratedValue; 
import javax.persistence.GenerationType; 
import javax.persistence.Id; 
import javax.persistence.JoinColumn; 
import javax.persistence.JoinTable; 
import javax.persistence.ManyToMany; 
import javax.persistence.OneToOne; 
import javax.persistence.Table; 
import javax.persistence.Transient; 

import org.apache.commons.beanutils.BeanUtils; 
import org.hibernate.annotations.Cascade; 
import org.hibernate.validator.Length; 
import org.hibernate.validator.NotEmpty; 
import org.hibernate.validator.NotNull; 

import com.yob.dps.vo.UserRolesVO; 
import com.yob.dps.vo.UserVO; 

/** 
* User. 
* Created by First Base Business Architect: Automation of Software Development 
* 
* @author - vyankatesh.deshmukh 
*/ 
@Entity 
@Table(name = "user") 
public class User extends BaseModel implements Serializable, Comparable<User>, 
     Cloneable { 
    private static final long serialVersionUID = 1L; 

    /** primary key */ 
    private String userId; 

    private String firstName; 

    private String middleName; 

    private String lastName; 

    private String phoneNumber; 

    private String mobileNumber; 

    private String openID; 

    private String emailId; 

    private String status; 

    private Date createdDateTime; 

    private Date updatedDateTime; 

    private UserRoles userRoles; 

    private Set<Subscriptions> subscriptionses = new HashSet<Subscriptions>(0); 

    @ManyToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY) 
    @JoinTable(name="usersubscriptions", catalog="scanbook", joinColumns = { 
    @JoinColumn(name="UserId", nullable=false, updatable=false) }, inverseJoinColumns = { 
    @JoinColumn(name="SubscriptionsId", nullable=false, updatable=false) }) 
    @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE }) 
    public Set<Subscriptions> getSubscriptionses() { 
     return this.subscriptionses; 
    } 

    public void setSubscriptionses(Set<Subscriptions> subscriptionses) { 
     this.subscriptionses = subscriptionses; 
    } 


/** 
    * Getter for property userId. 
    * 
    * @return value of property userId 
    */ 
    @Id 
    @Column(name = "UserId", unique = true, nullable = false) 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    public String getUserId() 
    { 
    return this.userId; 
    } 

    /** 
    * Setter for property userId. 
    * 
    * @param userId 
    *   new value of property userId 
    */ 
    public void setUserId(String userId) { 
     this.userId = userId; 
    } 

    /** 
    * Getter for property firstName. 
    * 
    * @return value of property firstName 
    */ 
    @Column(name = "firstName", length = 255, nullable = false) 
    @NotEmpty 
    @Length(max = 255) 
    public String getFirstName() { 
     return this.firstName; 
    } 

    /** 
    * Setter for property firstName. 
    * 
    * @param firstName 
    *   new value of property firstName 
    */ 
    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    /** 
    * Getter for property middleName. 
    * 
    * @return value of property middleName 
    */ 
    @Column(name = "middleName", length = 255, nullable = true) 
    @Length(max = 255) 
    public String getMiddleName() { 
     return this.middleName; 
    } 

    /** 
    * Setter for property middleName. 
    * 
    * @param middleName 
    *   new value of property middleName 
    */ 
    public void setMiddleName(String middleName) { 
     this.middleName = middleName; 
    } 

    /** 
    * Getter for property lastName. 
    * 
    * @return value of property lastName 
    */ 
    @Column(name = "lastName", length = 255, nullable = false) 
    @NotEmpty 
    @Length(max = 255) 
    public String getLastName() { 
     return this.lastName; 
    } 

    /** 
    * Setter for property lastName. 
    * 
    * @param lastName 
    *   new value of property lastName 
    */ 
    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    /** 
    * Getter for property phoneNumber. 
    * 
    * @return value of property phoneNumber 
    */ 
    @Column(name = "phoneNumber", length = 55, nullable = true) 
    @Length(max = 55) 
    public String getPhoneNumber() { 
     return this.phoneNumber; 
    } 

    /** 
    * Setter for property phoneNumber. 
    * 
    * @param phoneNumber 
    *   new value of property phoneNumber 
    */ 
    public void setPhoneNumber(String phoneNumber) { 
     this.phoneNumber = phoneNumber; 
    } 

    /** 
    * Getter for property mobileNumber. 
    * 
    * @return value of property mobileNumber 
    */ 
    @Column(name = "mobileNumber", length = 55, nullable = true) 
    @Length(max = 55) 
    public String getMobileNumber() { 
     return this.mobileNumber; 
    } 

    /** 
    * Setter for property mobileNumber. 
    * 
    * @param mobileNumber 
    *   new value of property mobileNumber 
    */ 
    public void setMobileNumber(String mobileNumber) { 
     this.mobileNumber = mobileNumber; 
    } 

    /** 
    * Getter for property openID. 
    * 
    * @return value of property openID 
    */ 
    @Column(name = "openID", length = 255, nullable = true) 
    @Length(max = 255) 
    public String getOpenID() { 
     return this.openID; 
    } 

    /** 
    * Setter for property openID. 
    * 
    * @param openID 
    *   new value of property openID 
    */ 
    public void setOpenID(String openID) { 
     this.openID = openID; 
    } 

    /** 
    * Getter for property emailId. 
    * 
    * @return value of property emailId 
    */ 
    @Column(name = "emailId", length = 255, nullable = false) 
    @NotEmpty 
    @Length(max = 255) 
    public String getEmailId() { 
     return this.emailId; 
    } 

    /** 
    * Setter for property emailId. 
    * 
    * @param emailId 
    *   new value of property emailId 
    */ 
    public void setEmailId(String emailId) { 
     this.emailId = emailId; 
    } 

    /** 
    * Getter for property status. 
    * 
    * @return value of property status 
    */ 
    @Column(name = "status", length = 1, nullable = true) 
    @Length(max = 1) 
    public String getStatus() { 
     return this.status; 
    } 

    /** 
    * Setter for property status. 
    * 
    * @param status 
    *   new value of property status 
    */ 
    public void setStatus(String status) { 
     this.status = status; 
    } 

    /** 
    * Getter for property createdDateTime. 
    * 
    * @return value of property createdDateTime 
    */ 
    @Column(name = "createdDateTime", nullable = false) 
    @NotNull 
    public Date getCreatedDateTime() { 
     return this.createdDateTime; 
    } 

    /** 
    * Setter for property createdDateTime. 
    * 
    * @param createdDateTime 
    *   new value of property createdDateTime 
    */ 
    public void setCreatedDateTime(Date createdDateTime) { 
     this.createdDateTime = createdDateTime; 
    } 

    /** 
    * Getter for property updatedDateTime. 
    * 
    * @return value of property updatedDateTime 
    */ 
    @Column(name = "updatedDateTime", nullable = false) 
    @NotNull 
    public Date getUpdatedDateTime() { 
     return this.updatedDateTime; 
    } 

    /** 
    * Setter for property updatedDateTime. 
    * 
    * @param updatedDateTime 
    *   new value of property updatedDateTime 
    */ 
    public void setUpdatedDateTime(Date updatedDateTime) { 
     this.updatedDateTime = updatedDateTime; 
    } 

    /** 
    * Getter for property userRoles. 
    * 
    * @return value of property userRoles 
    */ 
    @JoinColumn(name = "UserRolesId", referencedColumnName = "UserRolesId", nullable = false) 
    @OneToOne(cascade = { CascadeType.PERSIST, CascadeType.MERGE }) 
    public UserRoles getUserRoles() { 
     return this.userRoles; 
    } 

    /** 
    * Setter for property userRoles. 
    * 
    * @param userRoles 
    *   new value of property userRoles 
    */ 
    public void setUserRoles(UserRoles userRoles) { 
     this.userRoles = userRoles; 
    } 

    /** 
    * constructor - Default constructor which loads defaults by using method loadDefaults 
    */ 
    public User() { 
     this(true); 
    } 

    /** 
    * constructor - Constructor with option to load defaults 
    * 
    * @param loadDefaults 
    */ 
    public User(boolean loadDefaults) { 
     if (loadDefaults) { 
      loadDefaults(); 
     } 
    } 

    /** 
    * constructor - Constructor with primary key as parameter 
    * 
    * @param userId 
    */ 
    public User(String userId) { 
     this.userId = userId; 
    } 



    /** 
    * equals - 
    * 
    * @param other 
    * @return boolean 
    */ 
    public boolean equals(final Object other) { 
     if (other == null) 
      return false; 
     if (this == other) 
      return true; 
     if (!(other instanceof User)) 
      return false; 

     final User user = (User) other; 
     if (this.userId != null && user.getUserId() != null) { 
      return equalsByPK(user); 
     } else { 
      return equalsByBK(user); 
     } 
    } 

    /** 
    * equalsByPK - Return true if primary keys are equal (and not 0), otherwise false 
    * 
    * @param user 
    * @return boolean 
    */ 
    public boolean equalsByPK(final User user) { 
     return this.getUserId().equals(user.getUserId()); 
    } 

    /** 
    * equalsByBK - Returns true if all required attribures are equal, otherwise return false 
    * 
    * @param user 
    * @return boolean 
    */ 
    public boolean equalsByBK(final User user) { 
     if (this.getFirstName() != null 
       && !this.getFirstName().equals(user.getFirstName())) 
      return false; 
     if (this.getLastName() != null 
       && !this.getLastName().equals(user.getLastName())) 
      return false; 
     if (this.getEmailId() != null 
       && !this.getEmailId().equals(user.getEmailId())) 
      return false; 
     if (this.getCreatedDateTime() != null 
       && !this.getCreatedDateTime().equals(user.getCreatedDateTime())) 
      return false; 
     if (this.getUpdatedDateTime() != null 
       && !this.getUpdatedDateTime().equals(user.getUpdatedDateTime())) 
      return false; 

     return true; 
    } 

    /** 
    * hashCode - 
    * 
    * @return integer 
    */ 
    public int hashCode() { 
     int result; 
     if (getUserId() != null) { 
      // fast and reliable: when there's a primary key, use it as hashcode 
      result = getUserId().hashCode(); 
     } else { 
      result = 14; 
      if (firstName != null) 
       result = 29 * result + this.getFirstName().hashCode(); 
      if (middleName != null) 
       result = 29 * result + this.getMiddleName().hashCode(); 
      if (lastName != null) 
       result = 29 * result + this.getLastName().hashCode(); 
      if (phoneNumber != null) 
       result = 29 * result + this.getPhoneNumber().hashCode(); 
      if (mobileNumber != null) 
       result = 29 * result + this.getMobileNumber().hashCode(); 
      if (openID != null) 
       result = 29 * result + this.getOpenID().hashCode(); 
      if (emailId != null) 
       result = 29 * result + this.getEmailId().hashCode(); 
      if (status != null) 
       result = 29 * result + this.getStatus().hashCode(); 
      if (createdDateTime != null) 
       result = 29 * result + this.getCreatedDateTime().hashCode(); 
      if (updatedDateTime != null) 
       result = 29 * result + this.getUpdatedDateTime().hashCode(); 

      if (userRoles != null) 
       result = 17 * result + userRoles.hashCode(); 
     } 
     return result; 
    } 

    /** 
    * toString - 
    * 
    * @return String 
    */ 
    public String toString() { 
     StringBuilder result = new StringBuilder("User: "); 

     result.append("firstName=" + firstName); 

     result.append(", "); 

     result.append("middleName=" + middleName); 

     result.append(", "); 

     result.append("lastName=" + lastName); 

     result.append(", "); 

     result.append("phoneNumber=" + phoneNumber); 

     result.append(", "); 

     result.append("mobileNumber=" + mobileNumber); 

     result.append(", "); 

     result.append("openID=" + openID); 

     result.append(", "); 

     result.append("emailId=" + emailId); 

     result.append(", "); 

     result.append("status=" + status); 

     result.append(", "); 

     result.append("createdDateTime=" + createdDateTime); 

     result.append(", "); 

     result.append("updatedDateTime=" + updatedDateTime); 

     return result.toString(); 
    } 

    /** 
    * copyInto - 
    * 
    * @param targetUser 
    */ 
    public void copyInto(final UserVO targetUser) 
    { 
    targetUser.setFirstName(this.getFirstName()); 
    targetUser.setMiddleName(this.getMiddleName()); 
    targetUser.setLastName(this.getLastName()); 
    targetUser.setPhoneNumber(this.getPhoneNumber()); 
    targetUser.setMobileNumber(this.getMobileNumber()); 
    targetUser.setOpenID(this.getOpenID()); 
    targetUser.setEmailId(this.getEmailId()); 
    targetUser.setStatus(this.getStatus()); 
    targetUser.setCreatedDateTime(this.getCreatedDateTime()); 
    targetUser.setUpdatedDateTime(this.getUpdatedDateTime()); 
    targetUser.setUserId(this.getUserId()); 
    UserRolesVO userRolesVO=new UserRolesVO(); 
    /*Set<SubscriptionsVO> subScSet=new HashSet<SubscriptionsVO>(); 
     try { 
      for(Subscriptions subscriptions:this.getSubscriptionses()){ 
       SubscriptionsVO subscriptionsVO=new SubscriptionsVO(); 
       BeanUtils.copyProperties(subscriptionsVO,subscriptions); 
       subScSet.add(subscriptionsVO); 
      } 
      targetUser.setSubscriptionses(subScSet);*/ 
    try { 
     BeanUtils.copyProperties(userRolesVO,this.getUserRoles()); 
     targetUser.setUserRoles(userRolesVO); 
    } catch (IllegalAccessException e) { 
     e.printStackTrace(); 
    } catch (InvocationTargetException e) { 
     e.printStackTrace(); 
    } 
    } 

    /** 
    * createLabel - Create a short representation of User which 
    * can be used in combo's 
    * 
    * @param messages 
    * @return String 
    */ 
    @Transient 
    public String createLabel(final Map<String, String> messages) { 
     String result; 
     result = messages.get("user.label.format"); 
     return result; 
    } 

    /** 
    * loadDefaults - load default values of some fields 
    */ 
    public void loadDefaults() { 

    } 

} 

但在執行程序後,我收到上面提到的異常。

我的代碼有什麼問題?提前致謝。

+0

只是胡亂猜測,也許用戶它的用戶。 – wxyz

+0

如果不仔細查看地圖,我們將無法幫助您。請發佈'用戶'類。 –

+0

@Alexey Malev新增用戶類別 – Shruti

回答

2

最有可能的問題是from子句。你應該利用你的實體名稱,實體名稱和屬性在JPQL是區分大小寫的:

.... 
from User u, Subscriptions s, UserSubscriptions us 
where  
.... 

你也很可能會遇到的問題與屬性名稱。在查詢所有的人都大寫:

where 
    u.openID = :openId and 
    u.UserId = us.UserId and 

但是,JPA提供商很可能會想到userId,不UserId

-1

hibernate.cfg.xml添加用戶映射表如下所示:

<hibernate-mapping>................ 
<mapping class="com.model.User"></mapping> 
+0

@Sirisha - 提示:除非您在工具欄中使用代碼按鈕「{}」,否則XML在瀏覽器中不可見,或者使用四(4)個空格縮進行。 – Leigh