使用具有复杂条件的JPA实体图 [英] Using JPA entity graph with complex conditions

查看:158
本文介绍了使用具有复杂条件的JPA实体图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Spring MVC + JPA + Hibernate的项目。我使用实体图(JPA 2.1)来定义从数据库中提取哪些数据,如下面的示例中所示。

I have a project built on Spring MVC + JPA + Hibernate. I am using entity graphs (JPA 2.1) for defining which data to fetch from the database, like in the example below.

EntityGraph<Company> entityGraph = entityManager.createEntityGraph(Company.class);
entityGraph.addAttributeNodes("reviews");

Map<String, Object> hints = new HashMap<String, Object>();
hints.put("javax.persistence.loadgraph", entityGraph);

Company company = entityManager.find(Company.class, companyId, hints);

我的复习实体与a 公司实体( ManyToOne )。

My Review entity has an association with a Company entity (ManyToOne).

使用填充的评论集合获取公司对象。这在上述场景中运行良好。但是如果我想获取给定公司的全部或部分评论呢?也就是说,与具有给定ID的公司相关联的 Review 对象。我想要一个 List< Review> 而不是 Company 对象,其中 List< Review> 。这只是一个例子 - 基本上我正在寻找更多的灵活性,而不仅仅是基于主键查找对象。我可以用HQL来完成,没问题,但是我必须根据我在特定上下文中需要的数据编写几个类似的查询。

Here I simply fetch a Company object with a populated reviews collection. This works well in scenarios like the above. But what if I want to fetch all or some of the reviews of a given company? That is, the Review objects that are associated with a company with a given ID. I want a List<Review> instead of a Company object with a List<Review>. This is just an example - basically I am looking for more flexibility than simply looking up an object based on a primary key. I can do it with HQL with no problem, but then I would have to write several similar queries depending on which data I need in a specific context.

> javax.persistence.EntityManager 上查找方法可以简单地查询基于主键的对象。但是在某些更复杂的场景中是否可以使用实体图与Criteria对象或HQL查询?例如,使用其他条件而不是主键查找对象 - 甚至可能是关联条件。

The find method on javax.persistence.EntityManager simply makes it possible to query for an object based on a primary key. But is it somehow possible to use entity graphs in more complicated scenarios, e.g. with Criteria objects or HQL queries? For example, looking up objects with other conditions than by primary key - perhaps even conditions on associations.

我希望我明确自己。提前致谢!

I hope I made myself clear. Thanks in advance!

推荐答案

您正在寻找的可能不是 EntityGraph ,但JPA Query (或者JPQL形式的 NamedQuery CriteriaQuery )。这是所有JPA规范的一部分。

What you are looking for is probably not EntityGraphs, but JPA Query (either JPQL in form of a NamedQuery or a CriteriaQuery). This is all part of the JPA specification.

所以基本上你可以:

So basically you can:


  1. 使用 @NamedQueries 注释每个实体类以指定JPQL查询。它们的优点是它们的语法在部署时被检查(部署将失败,如果NamedQueries访问缺少的属性)并且可重用和缺点是:它们是静态定义的(但当然接受参数)。
  2. 使用EntityManager构建运行时JPQL查询。由于上述优点,我比使用运行时查询更经常使用NamedQuery。

  3. 在加入/搜索/添加条件时,使用Criteria API有其优点,它们是类型安全的/使用真正的Java对象。

  1. Annotate each entity clas with @NamedQueries to specify JPQL queries. Their advantage is that their syntax is checked on deployment (deployment will fail if e.g. the NamedQueries access missing properties) and are reusable and disadvantage is: they are statically defined (but of course accept parameters).
  2. Construct on runtime JPQL queries with EntityManager. I use NamedQueries more often than runtime queries because of the above mentioned advantages.
  3. Use the Criteria API, which has the advantage, they are type-safe, as you join/search/add conditions/play their with real Java Objects.

现在关于 EntityGraph s:只是一个帮助,以便从查询中获取附加字段(无论您使用 EntityManager.find()以及其他属性映射参数或 Query.setHints() )。你也可以使用子图来处理更复杂的情况。检查这个例子

Now about EntityGraphs: they are just a help so fetch additional fields from a query (no matter if you use EntityManager.find() with the additional properties map parameter or Query.setHints()). You could also use Subgraphs for more complex situations. Check this and this example.

这篇关于使用具有复杂条件的JPA实体图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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