JPA 2.1中的条件获取联接中的导航方法 [英] Navigation method/s in criteria fetch joins in JPA 2.1

查看:111
本文介绍了JPA 2.1中的条件获取联接中的导航方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然在JPA标准中使用访存联接,但看不到任何导航方法.下面给出一个例子.

While using fetch joins in JPA criteria, there is no navigation method can be seen. Given below an example.

Root<UserTable> root = criteriaQuery.from(entityManager.getMetamodel().entity(UserTable.class));
Fetch<UserTable, StateTable> fetch = root.fetch(UserTable_.stateTable, JoinType.INNER);

要浏览所涉及的实体,必须将这种Fetch类型强制转换为联接,如下所示.

To navigate through the entity in question, this Fetch type is required to be cast to a join like as follows.

Join<UserTable, StateTable> join = (Join<UserTable, StateTable>) root.fetch(UserTable_.stateTable, JoinType.INNER);

此投射后,导航方法get()可用,就像

The navigation method get() is available after this cast just like,

join.get(UserTable_.firstName);

这种类型是否可移植(在EclipseLink(2.5.1)和Hibernate(4.3.5)上都可以使用)?还有其他方法可以做到这一点吗?为什么标准获取联接不支持导航方法有特定的原因吗?

Is this type cast portable (while it works on EclipseLink (2.5.1) and Hibernate (4.3.5))? Is there any other ways to do the same? Is there any specific reason as to why criteria fetch joins do not support a navigation method?

更新: (我仍然认为这只是部分答案.因此,未写在答案部分)

James 在"nofollow noreferrer>此处"中,介绍了为什么FETCH JOIN中的导航方法(包括强烈建议不要使用JPQL),尤其在OneToMany关系中不建议使用JPQL(因此在FETCH JOIN s中没有直接提供/支持):

This is very well spotted on here by James as to why a navigation method in FETCH JOIN (including aliasing in JPQL) is highly discouraged and not recommended especially in OneToMany relationships (and consequently not directly provided/supported in FETCH JOINs):

EclipseLink允许您在JOIN FETCH上使用别名.这种支持 旨在用于OneToOneManyToOne关系,以避免 加入它两次只是为了获得一个别名,并允许在其中使用它 ORDER BY或以其他方式不会过滤结果和 更改对象的构建方式.但是,没有什么可以阻止你 与OneToMany一起使用来过滤获取的内容 OneToMany结果.

EclipseLink allows you to use an alias on a JOIN FETCH. This support was intended for OneToOne and ManyToOne relationships, to avoid having to join it twice just to get an alias, as well as to allow using it in an ORDER BY or in other ways that would not filter the results and alter how the objects are built. But, there is nothing stopping you from using it with a OneToMany to filter the contents of the fetched OneToMany results.

您可能希望阅读带有给定示例的整个博客.以我的经验,Hibernate(最新版本)还允许我们执行此操作.

You may want to read up the entire blog with the given examples instead. To my experience, Hibernate (at latest versions) also allows us to do this.

通常,我个人不使用OneToMany关系中的别名来浏览目标实体(具有外键的实体),实际上在实际项目中充其量是不必要的,但是在和/或ManyToOne.

In general, I personally do not use aliasing in OneToMany relationships to navigate through the target entity (the one which has the foreign key) that indeed should not be needed in real projects at best but it is required in OneToOne and/or ManyToOne.

推荐答案

您可以致电

Join<UserTable, StateTable> join = root.join("prop", JoinType.INNER);

请参阅此处: http://docs.oracle.com/javaee/6/api/javax/persistence/criteria/From.html#join(java.lang.String,%20javax.persistence.criteria.JoinType)

这篇关于JPA 2.1中的条件获取联接中的导航方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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