我正在尝试通过 hibernate createQuery 从映射器表中获取对象,但出现此错误。
错误详情
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 + " ]";
}
}
休眠.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>
请帮帮我
提前致谢