Java 从@ElementCollection获取字段的HQL查询
我正在尝试构造一个HQL查询,以从@ElementCollection中提取属性:Java 从@ElementCollection获取字段的HQL查询,java,hibernate,hql,Java,Hibernate,Hql,我正在尝试构造一个HQL查询,以从@ElementCollection中提取属性: public class Resource { @Id private String name; @ElementCollection() @CollectionTable( name = "property", ) @SortNatural private final SortedMap<String, String> propert
public class Resource {
@Id
private String name;
@ElementCollection()
@CollectionTable(
name = "property",
)
@SortNatural
private final SortedMap<String, String> properties =
Maps.newTreeMap();
}
这是可行的,当我调用q.list()
时,我会得到一个包含名称和值的对象列表。我遇到的问题是,我不知道如何获取与值关联的键。i、 e.properties\u键
列中的数据
我尝试过的事情:
有可能把这些数据拿出来吗?如何计算要在HQL查询中使用的属性的名称?加入集合,然后使用index()获取密钥。
索引(p)是关键,而p是值
List list = currentSession.createQuery(
"select new list(s.id, index(p), p) from Resource s join s.properties p").list();
非常感谢你,我遇到了完全相同的问题,这就是我的解决方案。这完全是晦涩难懂的!
List list = currentSession.createQuery(
"select new list(s.id, index(p), p) from Resource s join s.properties p").list();