gpt4 book ai didi

java - Hibernate 条件查询抛出异常

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:10:34 28 4
gpt4 key购买 nike

我想获取平均评分低于 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)

...

我已将查询更改为 native 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" 是一个字符串值。所以,你得到一个异常(exception)是正常的。这也不是你想要的。您不能通过这种方式检查列之间的相等性。相反,使用:

Restrictions.eqProperty("r.shopId", "s.id")

关于java - Hibernate 条件查询抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29329707/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com