HQL查询以从@ElementCollection获取字段 [英] HQL Query to get fields from @ElementCollection
问题描述
我正在尝试构造一个HQL查询以从@ElementCollection中提取属性:
I'm trying to construct an HQL query to pull properties from an @ElementCollection:
public class Resource {
@Id
private String name;
@ElementCollection()
@CollectionTable(
name = "property",
)
@SortNatural
private final SortedMap<String, String> properties =
Maps.newTreeMap();
}
property
表使用默认列名(properties
和properties_key
)来存储数据(使用外键返回我的资源表).
The property
table is using the default column names (properties
and properties_key
) to store the data (with a foreign key back to my resource table).
我有以下HQL查询,正在尝试返回键和值.
I have the following HQL query, where I'm trying to return the keys and values.
final Query q = session.createQuery("select new list(r.name, elements(r.properties)) from Resource r where r.name like :nameFilter");
这是有效的,当我调用q.list()
时,我得到一个包含名称和值的对象列表.我遇到的问题是我无法弄清楚如何获取与值关联的键.即properties_key
列中的数据.
This is working, and when I call q.list()
, I get a List of objects containing the name and values. The problem I'm having is that I can't figure out how to get the keys which are associated with the values. i.e. The data in the properties_key
column.
我尝试过的事情:
- 元素(r.properties_key)
- 元素(r.propertiesKey)
- 元素(r.key)
没有一项工作.
是否可以获取此数据?如何确定要在HQL查询中使用的属性的名称?
Is it possible to get this data out? How can I figure out the name of the property to use in my HQL query?
推荐答案
加入集合,然后使用 index()获取密钥.
index(p)是键,而 p 是值.
Join the collection and then use index() to get the key.
index(p) is the key and p is the value.
List list = currentSession.createQuery(
"select new list(s.id, index(p), p) from Resource s join s.properties p").list();
这篇关于HQL查询以从@ElementCollection获取字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!