org.hibernate.hql.ast.QuerySyntaxException:未映射实体类

时间:2013-08-14 11:32:17

标签: hibernate hibernate-mapping

我正在尝试通过hibernate createQuery从mapper表中获取对象,但是我收到了这个错误。

错误详情

2013-08-14 15:33:44 DEBUG DispatcherServlet:671 - Could not complete request
org.hibernate.hql.ast.QuerySyntaxException: UsersEventsPK is not mapped [select u from        UsersEventsPK u  where u.userid = :userid and u.eventid = :eventid]
at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:180)
at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:111)

以下是执行HQL查询的部分

sessionFactory.getCurrentSession().createQuery("select u from UsersEventsPK u " +
              " where u.userid = :userid" +
              " and u.eventid = :eventid")
              .setParameter("userid", userid)
              .setParameter("eventid", eventid).uniqueResult();

以下是我的实体类

package com.cmc.aop.entity;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Embeddable;

  @Embeddable
  public class UsersEventsPK implements Serializable {
     @Basic(optional = false)
     @Column(name = "userid", nullable = false)
     private long userid;
     @Basic(optional = false)
     @Column(name = "eventid", nullable = false)
     private long eventid;

     public UsersEventsPK() {
     }

public UsersEventsPK(long userid, long eventid) {
    this.userid = userid;
    this.eventid = eventid;
}

public long getUserid() {
    return userid;
}

public void setUserid(long userid) {
    this.userid = userid;
}

public long getEventid() {
    return eventid;
}

public void setEventid(long eventid) {
    this.eventid = eventid;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (int) userid;
    hash += (int) eventid;
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof UsersEventsPK)) {
        return false;
    }
    UsersEventsPK other = (UsersEventsPK) object;
    if (this.userid != other.userid) {
        return false;
    }
    if (this.eventid != other.eventid) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.cmc.aop.entity.UsersEventsPK[ userid=" + userid + ", eventid=" + eventid + " ]";
}

}

hibernate.cfg.xml中

<hibernate-configuration>

<session-factory>

    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping class="com.cmc.aop.entity.UsersEvents" />
    <mapping class="com.cmc.aop.entity.UsersEventsPK" />

</session-factory>

</hibernate-configuration>

请帮帮我

提前致谢

0 个答案:

没有答案