我的对象的返回列表有问题。我正在使用实体类,@EmbeddedId
正如您在下面的代码中看到的那样,有两行具有相似的文本:id.*
.
Criteria cr = createCriteria(true);
cr.add(Restrictions.eq("id.qwerty", "QWERTY"));
ProjectionList projectionList = Projections.projectionList();
projectionList.add(Projections.property("id.qwerty"));
projectionList.add(Projections.property("text"));
projectionList.add(Projections.property("id.property"));
cr.setProjection(projectionList);
cr.setResultTransformer(Transformers.aliasToBean(My.class));
List<My> myList = (List<My>) cr.list();
return myList;
在行List<My> messagesList = (List<My>) cr.list();
发生以下错误。
java.lang.ClassCastException: my.package.dao.domain.My cannot be cast to [Ljava.lang.Object;
at org.hibernate.cache.StandardQueryCache.put(StandardQueryCache.java:104)
at org.hibernate.loader.Loader.putResultInQueryCache(Loader.java:2274)
at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2206)
at org.hibernate.loader.Loader.list(Loader.java:2164)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1706)
at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
我该如何定义Criteria
或ProjectionList
在这种情况下?
编辑
@javax.persistence.Entity
@Table(name = "my_table")
public class My extends MyEntity<MyPK> {
@EmbeddedId
private MyPK id;
@Column(name = "text", nullable = false)
protected String text;
@ManyToOne
@JoinColumn(name = "property", nullable = false, insertable = false, updatable = false)
protected Option option;
@Override
public MyPK getId() {
return id;
}
@Override
public void setId(MyPK id) {
this.id = id;
}
//getters and setter
//equlas and hashCode
}
和MyPK
类:
@Embeddable
public class MyPK implements Serializable {
@Column(name = "qwerty")
protected String qwerty;
@Column(name = "property")
protected String property;
//constructors, getters and setters
}