使用HQL选择集合 [英] Select collections with HQL

查看:89
本文介绍了使用HQL选择集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类:

Person.java

  class Person {
String name;
设置< Hotel> visitedHotels;
String someOtherData;

public Person(){}
$ b $ public Person(String name,Set< Hotel> visitsHotels){
this.name;
this.visitedHotels = this.visitedHotels;
}

// getters& setters
}

Hotel.java

  class Hotel {
//一些代码
}

为了安全起见,someOtherData有时候不应该被加载。

所以我尝试了下面的HQL:

p>

 从Person p 
中选择新的Person(p.name,elements(p.visitedHotels))

 选择新人(p。名称,酒店)from Person p left join p.visitedHotels hotels 

但它不起作用 - 错误:无法在类Person上找到合适的构造函数。



是否可以选择酒店集合以及人名?


<使用集合映射查看Blaze-Persistence实体视图。解决方案

这可能只是你正在寻找的东西: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#collection-mappings



允许拥有单独的DTO就像

  @EntityView(Person.class)
interfae PersonView {
String getName();
设置< Hotel> getVisitedHotels();
}

查询中的用法,如

CriteriaBuilder< PersonView> cb = entityViewManager.apply(
EntityViewSetting.create(PersonView.class),
criteriaBuilderFactory.create(Person.class)
);

列表< PersonView> list = cb.getResultList();

创建一个查询,如

  SELECT person.name,visitedHotels_1 FROM Person person LEFT JOIN person.visitedHotels visitedHotels_1 

因为你似乎正在使用它来进行可视化,所以我建议将Hotel也映射为实体视图。


I have the following classes:

Person.java

class Person {
    String name;
    Set<Hotel> visitedHotels;
    String someOtherData;

    public Person() {}

    public Person(String name, Set<Hotel> visitedHotels) {
        this.name;
        this.visitedHotels = this.visitedHotels;
    }

    // getters & setters
}

Hotel.java

class Hotel {
    // some code 
}

For security reasons "someOtherData" should sometimes not be loaded.

So I tried the following HQL:

select new Person( p.name , elements(p.visitedHotels) ) from Person p

or

select new Person( p.name , hotels ) from Person p left join p.visitedHotels hotels

But it doesn’t work - error: Unable to locate appropriate constructor on class Person.

Is there a possibility to select the collection of hotels together with the person name?

解决方案

Take a look at Blaze-Persistence Entity Views with collection mappings. This might just be what you are looking for: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#collection-mappings

Allows to have a separate DTO like

@EntityView(Person.class)
interfae PersonView {
    String getName();
    Set<Hotel> getVisitedHotels();
}

Usage in the query like

CriteriaBuilder<PersonView> cb = entityViewManager.apply(
    EntityViewSetting.create(PersonView.class),
    criteriaBuilderFactory.create(Person.class)
);

List<PersonView> list = cb.getResultList();

Creates a query like

SELECT person.name, visitedHotels_1 FROM Person person LEFT JOIN person.visitedHotels visitedHotels_1

Since you seem to be using this for visualization, I recommend mapping Hotel as entity view too.

这篇关于使用HQL选择集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆