How to make a JPA query use primitive ID instead of managed instance?

I tried but I couldn't find a better title so it looks weird but the question is actually very simple...

I have a JPA query which uses a foreigner key, by default JPA takes the managed entity as foreigner key to execute the queries, is there any way I can do it passing the long id instead?

EXAMPLE:

@Entity
@Table(name = "user")
public class User   {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, orphanRemoval = true, mappedBy = "user")
    private Set<Item> entries = new HashSet<>();
    
}
@Entity
@Table(name = "item") 
public class Item implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
 
    @ManyToOne(targetEntity = User.class, optional = false, fetch = FetchType.LAZY)
    private User user;
    
}
@Repository
public interface ItemRepository extends PagingAndSortingRepository<User, Long> {
    
        // @formatter:off
        @Query("SELECT item FROM Item item WHERE (:user is null OR :user = item.user)")
        Page<Item> findByUser(@Param("user")User user, Pageable pageable);
        //@formatter:on
    
    }

What I want is instead of passing the user entity to the repository class, pass only its id, this way i can take id as parameter and avoid one more query on database just to retrieve the user from its id

how can i do that


Well simply make the parameter as Long and then

@Query("SELECT item FROM Item item WHERE (:userId is null OR :userId = item.user.id)")
Page<Item> findByUser(@Param("userId")Long userId, Pageable pageable);