本文介绍了Hibernate元组条件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用第9章

与我尝试的不同之处在于,我使用的是Spring MVC 3.0.这是我的Address类以及我创建的方法.

The difference with my attempt is I am using spring MVC 3.0. Here is my Address class along with the method i created.

@RooJavaBean
@RooToString
@RooEntity
@RooJson
public class Address {

    @NotNull
    @Size(min = 1)
    private String street1;

    @Size(max = 100)
    private String street2;

    private String postalcode;

    private String zipcode;

    @NotNull
    @ManyToOne
    private City city;

    @NotNull
    @ManyToOne
    private Country country;

    @ManyToOne
    private AddressType addressType;

    @Transient
    public static List<Tuple> jqgridAddresses(Long pID){
        CriteriaBuilder builder = Address.entityManager().getCriteriaBuilder();
        CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
        Root<Address> addressRoot = criteria.from( Address.class );
        criteria.multiselect(addressRoot.get("id"), addressRoot.get("street1"), addressRoot.get("street2"));
        criteria.where(builder.equal(addressRoot.<Set<Long>>get("id"), pID));
        return Address.entityManager().createQuery( criteria ).getResultList();
    }
}

上面称为jqgridAddresses的方法是焦点.我选择不使用"路径" ,因为当我说 Path idPath = addressRoot.get(Address_.id); 之类的内容时,如文档第9.2节所述, PathAddress_.id 东西会产生编译错误.

The method called jqgridAddresses above is the focus. I opted not to use the "Path" because when I say something like Path idPath = addressRoot.get( Address_.id ); as in section 9.2 of the documentation, the PathAddress_.id stuff produces a compilation error.

上面的方法返回一个Tuple类型的空列表,因为它的大小为零,即使它应该包含某些内容.这表明查询失败.有人可以告诉我吗?

The method above returns an empty list of type Tuple as its size is zero even when it should contain something. This suggests that the query failed. Can someone please advise me.

推荐答案

好的,所以我对我的逻辑进行了一些细微的调整,这些逻辑是针对我的项目的,但是,以下方法可以很好地工作.希望它可以帮助需要帮助的人!

OK so i made some minor adjustments to my logic which is specific to my project, however, the following approach worked perfectly. Hope it hepls someone in need !

@Transient
public static List<Tuple> jqgridPersons(Boolean isStudent, String column, String orderType, int limitStart, int limitAmount){
    CriteriaBuilder builder = Person.entityManager().getCriteriaBuilder();
    CriteriaQuery<Tuple> criteria = builder.createTupleQuery();
    Root<Person> personRoot = criteria.from(Person.class );
    criteria.select(builder.tuple(personRoot.get("id"), personRoot.get("firstName"), personRoot.get("lastName"), personRoot.get("dateOfBirth"), personRoot.get("gender"), personRoot.get("maritalStatus")));
    criteria.where(builder.equal( personRoot.get("isStudent"), true));
    if(orderType.equals("desc")){
        criteria.orderBy(builder.desc(personRoot.get(column)));
    }else{
        criteria.orderBy(builder.asc(personRoot.get(column)));
    }
    return Address.entityManager().createQuery( criteria ).setFirstResult(limitStart).setMaxResults(limitAmount).getResultList();
}

这篇关于Hibernate元组条件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 22:39