将包含连接提取的 SQL 查询转换为 Criteria Hibernate 查询
Convert an SQL Query that includes join fetch to a Criteria Hibernate query
我正在尝试转换以下查询:
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");
标准 Hibernate 查询,但肯定遗漏了一些东西,因为我收到以下错误:
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);
抱歉 table/column 命名,但它是敏感数据。
我最终使用相同的查询解决了问题:
Root<TestEntity> testEntityRoot = criteria.from(TestEntity.class);
testEntityRoot.fetch(TestEntity_.someEntity, JoinType.INNER);
testEntityRoot.fetch(TestEntity_.someEntity2, JoinType.LEFT).fetch(SomeEntity2.someEntity3, JoinType.LEFT);
除了我以错误的顺序获取实体。
首先我们需要获取根实体,然后链接任何获取以获取我们想要的实体。
我正在尝试转换以下查询:
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");
标准 Hibernate 查询,但肯定遗漏了一些东西,因为我收到以下错误:
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);
抱歉 table/column 命名,但它是敏感数据。
我最终使用相同的查询解决了问题:
Root<TestEntity> testEntityRoot = criteria.from(TestEntity.class);
testEntityRoot.fetch(TestEntity_.someEntity, JoinType.INNER);
testEntityRoot.fetch(TestEntity_.someEntity2, JoinType.LEFT).fetch(SomeEntity2.someEntity3, JoinType.LEFT);
除了我以错误的顺序获取实体。
首先我们需要获取根实体,然后链接任何获取以获取我们想要的实体。