Hibernate条件查询引发异常

时间:2015-03-29 13:29:21

标签: java sql hibernate hibernate-criteria

我希望所有评级平均值低于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));

1 个答案:

答案 0 :(得分:4)

使用:

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

检查shopId是否等于字符串值"s.id"。但shopId是整数列,"s.id"是字符串值。所以,通常你得到一个例外。它也不是你想要的。您无法通过这种方式检查列之间的相等性。相反,使用:

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