{"action":"closed","issue":{"url":"https://api.github.com/repos/Blazebit/blaze-persistence/issues/238","repository_url":"https://api.github.com/repos/Blazebit/blaze-persistence","labels_url":"https://api.github.com/repos/Blazebit/blaze-persistence/issues/238/labels{/name}","comments_url":"https://api.github.com/repos/Blazebit/blaze-persistence/issues/238/comments","events_url":"https://api.github.com/repos/Blazebit/blaze-persistence/issues/238/events","html_url":"https://github.com/Blazebit/blaze-persistence/issues/238","id":177653458,"number":238,"title":"Question about Fetch joins for non-root entities","user":{"login":"JWGmeligMeyling","id":3469492,"avatar_url":"https://avatars.githubusercontent.com/u/3469492?v=3","gravatar_id":"","url":"https://api.github.com/users/JWGmeligMeyling","html_url":"https://github.com/JWGmeligMeyling","followers_url":"https://api.github.com/users/JWGmeligMeyling/followers","following_url":"https://api.github.com/users/JWGmeligMeyling/following{/other_user}","gists_url":"https://api.github.com/users/JWGmeligMeyling/gists{/gist_id}","starred_url":"https://api.github.com/users/JWGmeligMeyling/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/JWGmeligMeyling/subscriptions","organizations_url":"https://api.github.com/users/JWGmeligMeyling/orgs","repos_url":"https://api.github.com/users/JWGmeligMeyling/repos","events_url":"https://api.github.com/users/JWGmeligMeyling/events{/privacy}","received_events_url":"https://api.github.com/users/JWGmeligMeyling/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-09-18T14:00:22Z","updated_at":"2016-09-18T14:40:05Z","closed_at":"2016-09-18T14:40:05Z","body":"#I am trying to recursively traverse a tree (Scope) for which any node is attached to a collection of elements. These elements (`DiscountCode` here) have a back reference to their scope (mapped by). Using the following criteriabuilder, with an `innerJoin` and then a `select` I observe `n` queries being ran to fetch the `scope` of the `discountCode` (even though it's already in the result set from the Inner join. When trying to optimize this using a `fetch` (enabling the commented line below), I get the exception: \r\n\r\n```\r\nException in thread \"main\" java.lang.IllegalStateException: Fetch joins are only possible if the root entity is selected\r\n\tat com.blazebit.persistence.impl.AbstractFullQueryBuilder.checkFetchJoinAllowed(AbstractFullQueryBuilder.java:154)\r\n\tat com.blazebit.persistence.impl.AbstractFullQueryBuilder.fetch(AbstractFullQueryBuilder.java:162)\r\n\tat com.pallasathenagroup.portal.core.TestRun.main(TestRun.java:95)\r\n```\r\n\r\n```java\r\nval cb = cbf.create(entityManager, ScopeDiscountCode.class)\r\n    .from(ScopeCte.class)\r\n    .withRecursive(ScopeCte.class)\r\n        .from(Scope.class, \"scope\")\r\n        .bind(\"id\").select(\"scope.id\")\r\n        .bind(\"ancestor\").select(\"scope.parent\")\r\n        .where(\"scope\").eq(scope)\r\n    .unionAll()\r\n        .from(Scope.class, \"scope\")\r\n        .from(ScopeCte.class, \"parentScope\")\r\n        .bind(\"id\").select(\"scope.id\")\r\n        .bind(\"ancestor\").select(\"scope.parent\")\r\n        .where(\"scope.parent.id\").eqExpression(\"parentScope.id\")\r\n    .end()\r\n        .innerJoin(\"scope.discountCodes\", \"dc\")\r\n        .select(\"dc\")\r\n//        .fetch(\"dc.applicableIn\")\r\n        .getResultList();\r\n```\r\n\r\nIs there another way to express this in a CriteriaBuilder? Shouldn't the `@ManyToOne` relation be eagerly initialised anyway?"}}
{"actor":{"display_login":"JWGmeligMeyling"}}