休眠标准查询引发异常
作者:互联网
我想让所有商店的平均评级低于4.2.但是我的标准抛出ClassCastException.我有以下条件查询.
double temp = 4.2;
Criteria criteria = session.createCriteria(Shop.class, "s");
DetachedCriteria avg = DetachedCriteria.forClass(Rating.class, "r")
.setProjection(Projections.avg("r.value"))
.add(Restrictions.eq("r.shopId", "s.id"));
criteria.add(Subqueries.gt(temp, avg));
我的实体:
@Entity
@javax.persistence.Table(name = "shop")
public class Shop implements LazyClonable<Shop> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String name;
@Embedded
private Location location;
@Lob
private String description;
//...getters and setters
}
@Entity
@javax.persistence.Table(name = "rating")
public class Rating implements LazyClonable<Rating> {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private Float value;
private String comment;
@Transient
@ManyToOne(targetEntity = Shop.class)
@JoinColumn(name = "shop_id", insertable = false, updatable = false)
private Shop shop;
/** foreign key */
@Column(name = "shop_id")
private Integer shopId;
//...getters and setters
}
并引发异常.为什么?我做错了什么?
异常堆栈跟踪:
Mar 29, 2015 4:14:52 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SEVERE: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
at org.hibernate.type.descriptor.java.IntegerTypeDescriptor.unwrap(IntegerTypeDescriptor.java:36)
at org.hibernate.type.descriptor.sql.IntegerTypeDescriptor$1.doBind(IntegerTypeDescriptor.java:64)
at org.hibernate.type.descriptor.sql.BasicBinder.bind(BasicBinder.java:90)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:286)
at org.hibernate.type.AbstractStandardBasicType.nullSafeSet(AbstractStandardBasicType.java:281)
at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1995)
at org.hibernate.loader.Loader.bindParameterValues(Loader.java:1966)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1901)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doList(Loader.java:2554)
at org.hibernate.loader.Loader.doList(Loader.java:2540)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)
at org.hibernate.loader.Loader.list(Loader.java:2365)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:126)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1682)
…
我已将查询更改为本机SQL,但仍收到相同的异常:
criteria.add(Restrictions.sqlRestriction(
"(SELECT avg(r.value) FROM rating r WHERE s.id = r.shop_id) < ?", temp, DoubleType.INSTANCE));
解决方法:
带有:
Restrictions.eq("r.shopId", "s.id")
您检查shopId是否等于字符串值“ s.id”.但是shopId是整数列,“ s.id”是字符串值.因此,出现异常是正常的.而且它也不是你想要的.您不能以这种方式检查列之间的相等性.而是使用:
Restrictions.eqProperty("r.shopId", "s.id")
标签:hibernate,hibernate-criteria,sql,java 来源: https://codeday.me/bug/20191028/1953050.html