'JpaRepository custom @Query generates unwanted CROSS JOIN

account entity code:

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private User user;

user entity code:

    @OneToMany(mappedBy = "user", cascade = CascadeType.ALL)
    private List<Account> account = new ArrayList<>();

the repository cusotom method query annotion

@Query(value = "FROM Account a WHERE (:userId IS NULL Or a.user.id = :userId))

It generates this query:

from account account0_ cross join user user1_ where account0_.user_id=user1_.id and (? is null or user1_.id=?) 

It worked fine before I updating springboot from 2.1.4.release to 2.6.5

which is expected to be the following:

from account account0_ WHERE (:userId IS NULL Or account0_.user_id = :userId)

Don't know if it's due to the version change or not.

Can someone give some clues?



Solution 1:[1]

It's a stange behavior on JPA if you use

 @NotFound(action = NotFoundAction.IGNORE)

this above annotion, remove this will behave normally.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Yuzuha