将包含联接提取的SQL查询转换为Criteria Hibernate查询

时间:2019-02-25 21:24:23

标签: hibernate jpa hibernate-criteria

我正在尝试转换以下查询:

Query query = entityManager.createQuery("from TestEntity te " +
                    "join fetch te.someEntity se " +  
                    "left join fetch te.someEntity2 se2 "
                    "left join fetch se2.someEntity3 " +
                    "where se.predicateHere =:prediacte");

进入Criteria Hibernate Query,但肯定丢失了一些东西,因为我遇到以下错误:

query specified join fetching, but the owner of the fetched association was not present in the select list

当我尝试这样做时:

Root<TestEntity> testEntityRoot = criteria.from(TestEntity.class);
testEntityRoot.fetch(TestEntity_.someEntity, JoinType.INNER);
testEntityRoot.fetch(TestEntity_.someEntity2, JoinType.LEFT).fetch(SomeEntity2.someEntity3, JoinType.LEFT);

很抱歉,表/列的命名是敏感数据。

1 个答案:

答案 0 :(得分:0)

我最终使用相同的查询解决了这个问题:

Root<TestEntity> testEntityRoot = criteria.from(TestEntity.class);
testEntityRoot.fetch(TestEntity_.someEntity, JoinType.INNER);
testEntityRoot.fetch(TestEntity_.someEntity2, JoinType.LEFT).fetch(SomeEntity2.someEntity3, JoinType.LEFT);

除非我以错误的顺序获取实体。

首先,我们需要获取根实体,然后将所有获取链接起来以获取所需的实体。

相关问题