hibernate Panache查询具有不同的返回PanacheQuery< Entity>而不是ArrayList< String>

62o28rlo  于 2022-11-24  发布在  其他
关注(0)|答案(2)|浏览(248)

我试图用Panache+Hibernate从数据库中得到一个列的不同结果。通常在Hibernate中,你会从查询中得到一个ArrayList<String>

List<String> list = repo
            .find("select DISTINCT(a.country) from TMdBrandAll a order by a.country")
            .page(Page.ofSize(1000)).list();

但是,如果我在Panache中尝试这种方法,我会收到错误消息Comiler Error Message
如果我将变量“list”更改为returnType List<TMdBrandAll>,编译错误就消失了。

List<TMdBrandAll> list = brandAllRepo
            .find("select DISTINCT(a.country) from TMdBrandAll a order by a.country")
            .page(Page.ofSize(1000)).list();

现在,当我在调试器中检查已执行的代码时,我得到了. Debugger output
如何告诉Panache查询的结果是ArrayList<Strings>而不是ArrayList<PanacheEntity>
感谢您的回答
编辑:回购代码:

@RequestScoped
@Transactional
public class BrandAllRepo implements PanacheRepositoryBase<TMdBrandAll, Long> {

    public void updateBrand(String brandName, String soundexCode, String countryCode, Long active, Long isNew, String user, Long brandAllId) {

        update("set brandName = ?1, soundexCode = soundex(pkg_util.f_escape_special_char1(?2))," +
                " countryCode = ?3, active = ?4, isNew = ?5, modifiedAt = sysdate, modified_by = ?6 where brandAllId = ?7",
            brandName, soundexCode, countryCode, active, isNew, user, brandAllId);

    }

}

来自Repo的工作代码:

@Inject
EntityManager em;

public List<String> findCountries() {
    List<String> qres = em
        .createQuery("select DISTINCT(a.countryCode) from TMdBrandAll a order by a.countryCode", String.class)
        .getResultList();

    return new ArrayList<>(qres);
}

通过注入EntityManager和标准Hibernate查询,它可以正常工作。

toe95027

toe950271#

这是Panache的局限性。
看看代码https://github.com/quarkusio/quarkus/blob/master/extensions/panache/hibernate-orm-panache/runtime/src/main/java/io/quarkus/hibernate/orm/panache/PanacheRepositoryBase.java
它总是返回实体的List。
在BrandAllRepo中创建一个返回字符串列表的finder方法,或者使用一个无类型的列表:

List list = brandAllRepo
        .find("select DISTINCT(a.country) from TMdBrandAll a order by a.country")
        .page(Page.ofSize(1000)).list();

您知道列表中会有字符串。
第二个选择不太好。我会用第一个选择。

eyh26e7m

eyh26e7m2#

我也遇到了同样的问题,但我认为如果使用.project(String.class)GROUP BY而不是DISTINCT,它就可以工作:

List<String> list = repo
            .find("select a.country from TMdBrandAll a " 
                   + "group by a.country "
                   + "order by a.country")
            .project(String.class) 
            .page(Page.ofSize(1000)).list();

Panache似乎被DISTINCT子句搞糊涂了。

相关问题