简体   繁体   English

Hibernate 嵌套属性条件

[英]Hibernate nested properties criteria

Let's assume, each song belongs to exactly one album created by exactly one artist living in exactly one country.让我们假设,每首歌曲都只属于一个由居住在一个国家的艺术家创作的专辑。 A query like像这样的查询

createCriteria(Song.class)
    .add(Restrictions.eq("album.artist.country.id", 43)); 

fails and can be fixed using createAlias or createCriteria (like here ) so that the needed joins get performed.失败,可以使用createAliascreateCriteria (如此)修复,以便执行所需的连接。 I'm doing it and it works, but I'm missing some background:我正在这样做并且它有效,但我缺少一些背景:

Why does it work like this?为什么它会这样工作? Assuming no embedded properties, joining is the only choice, isn't it?假设没有嵌入属性,加入是唯一的选择,不是吗?

Concerning the join type , there's no ambiguity: It must be an INNER JOIN, otherwise the equality could not hold, right?关于连接类型,没有歧义:它必须是内部连接,否则等式不成立,对吧?

The questions about why something is built the way it is are always tricky, and you would probably need to ask authors directly about it.关于为什么要以这种方式构建某些东西的问题总是很棘手,您可能需要直接询问作者。

I was not able to find any official documentation about this, but I assume it has to do with String-based queries (HQL) vs object-based queries (Criteria).我找不到任何关于此的官方文档,但我认为它与基于字符串的查询 (HQL) 与基于对象的查询(标准)有关。

The query you wrote is anyway more HQL than Criteria, so why not use HQL for the entire query?无论如何,您编写的查询比 Criteria 更 HQL,那么为什么不为整个查询使用 HQL?

I understand that this is just a short example you wrote to describe the concept and that it may be convenient to use String-based path navigation joins in more complex Criteria queries also, but the point of Criteria is to provide more type-safe queries and reusable criteria parts which you can use to build the query in chunks, based on various other conditions that that influence what the final query will look like.我知道这只是您编写的一个简短示例来描述该概念,并且在更复杂的 Criteria 查询中使用基于字符串的路径导航连接可能也很方便,但 Criteria 的重点是提供更多类型安全的查询和可重用的标准部分,您可以根据影响最终查询外观的各种其他条件,使用它们来分块构建查询。

For example, say you have predefined constants for all of the entity properties so that you are sure you don't make any typos when working with String-based queries:例如,假设您为所有实体属性预定义了常量,以便确保在使用基于字符串的查询时不会出现任何拼写错误:

public final class Album_ {
  public static final String artist = "artist";
}

public final class Artist_ {
  public static final String country = "country";
}

There are tools/frameworks that generate these classes automatically.有一些工具/框架可以自动生成这些类。

So, you can generate a Criteria the following way:因此,您可以通过以下方式生成 Criteria:

createCriteria(Song.class)
 .createCriteria(Album_.artist)
 .createCriteria(Artist_.country)
 ...

For most queries the above approach is an unnecessary overhead and make the code less readable IMHO (I personally find HQL/JPQL more readable even when StringBuilder or similar is involved), but I think that's the basic idea behind Criteria: safer and more reusable query parts.对于大多数查询,上述方法是不必要的开销,并且会降低代码的可读性,恕我直言(我个人认为 HQL/JPQL 更具可读性,即使涉及StringBuilder或类似内容),但我认为这是 Criteria 背后的基本思想:更安全和更可重用的查询部分。

Concerning the join type, there's no ambiguity: It must be an INNER JOIN, otherwise the equality could not hold, right?关于连接类型,没有歧义:它必须是内部连接,否则等式不成立,对吧?

You should use INNER JOIN.您应该使用内部联接。

Why does it work like this?为什么它会这样工作?

Because hibernate generates, so you should put alias.因为hibernate会生成,所以你应该放别名。

Have you thought about Hibernate Native SQL instead?你有没有想过 Hibernate Native SQL 呢? Because there would be a messy query to database.因为会对数据库进行混乱的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM