omnifaces / omnipersistence

Utilities for JPA, JDBC and DataSources
Other
31 stars 12 forks source link

LEFT JOIN - QueryException: fetch not allowed in subquery from-elements #29

Closed Logicko closed 4 years ago

Logicko commented 4 years ago

Trying to use PagedDataModel and display this relationship : @OneToMany(mappedBy = "user", fetch = LAZY, cascade = ALL, orphanRemoval = true) @Cache(usage = TRANSACTIONAL) private List<LoginToken> loginTokens = new ArrayList<>();

@ManyToOne(optional = false) @Cache(usage = TRANSACTIONAL) private User user;

...in the dataTable as :

<k:column field="loginTokens.type" type="list" />

The data are loaded in this manner (backing bean) :

Map<Getter<User>, Object> criteria = new LinkedHashMap<>(); criteria.put(User::getId, Between.range(1L, 3L)); users = PagedDataModel.lazy(userService::getPageWithTokens).criteria(criteria).build();

...with getPageWithTokens() userService's method :

public PartialResultList<User> getPageWithTokens(Page page, boolean count) { return getPage(page, count, (builder, query, user) -> { user.fetch("loginTokens", JoinType.LEFT); }); }

Exception : javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.QueryException: fetch not allowed in subquery from-elements [select count(generatedAlias0) from org.example.kickoff.model.User as generatedAlias0 where generatedAlias0 in (select distinct generatedAlias1.id from org.example.kickoff.model.User as generatedAlias1 left join fetch generatedAlias1.loginTokens as generatedAlias2 where generatedAlias1.id between :id_0 and :id_1)]

If I change the join to : user.fetch("loginTokens");

The query is returning ONLY one record (there are three users, only one has one LoginToken)

Without a join on LoginToken I obtain the exception : org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.example.kickoff.model.User.loginTokens, could not initialize proxy - no Session

I want to select all users even if the record does not have a LoginToken...

Thanks

BalusC commented 4 years ago

It was already fixed 5 months ago in https://github.com/omnifaces/omnipersistence/commit/b63dd31e67ca6e7bbb09e248acfab7dfe0820b2a

I've created a 0.11 release with the fix. Once it's available in Maven, upgrade OmniPersistence in your project to that version.

<dependency>
    <groupId>org.omnifaces</groupId>
    <artifactId>omnipersistence</artifactId>
    <version>0.11</version>
</dependency>