I'm using springboot 2.1.2 and I encounter some problems with repository. Here are my entity classes:
@Entity @Getter @Setter @NoArgsConstructor @Table(name = "orders") public class Order { @Id @GeneratedValue private long id; @ManyToOne(targetEntity = User.class) private User user; //other fields and getters and setters are ignored } @Entity @Getter @Setter @NoArgsConstructor public class User { @Id private String email; //other fields and getters and setters are ignored } And my OrderRepository:
@Repository public interface OrderRepository extends JpaRepository { @Query("select o from Order o where o.user.email = ?1") List findAllByUserId(String userId); List findAllByUser(User user); } When I invoke findAllByUserId or findAllByUser, the repository returns a null value instead of an empty list, this is so strange since I'm sure that my database has data.
I've read other similar questions, and they don't seem to help.
I try to fix the problem with debugger, and I track into the AsyncExecutionInterceptor class:
@Nullable public Object invoke(MethodInvocation invocation) throws Throwable { Class targetClass = invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null; Method specificMethod = ClassUtils.getMostSpecificMethod(invocation.getMethod(), targetClass); Method userDeclaredMethod = BridgeMethodResolver.findBridgedMethod(specificMethod); AsyncTaskExecutor executor = this.determineAsyncExecutor(userDeclaredMethod); if (executor == null) { throw new IllegalStateException("No executor specified and no default executor set on AsyncExecutionInterceptor either"); } else { Callable task = () -> { try { Object result = invocation.proceed(); if (result instanceof Future) { return ((Future)result).get(); } } catch (ExecutionException var4) { this.handleError(var4.getCause(), userDeclaredMethod, invocation.getArguments()); } catch (Throwable var5) { this.handleError(var5, userDeclaredMethod, invocation.getArguments()); } return null; }; return this.doSubmit(task, executor, invocation.getMethod().getReturnType()); } } And I notice that in the 13rd line of this method, the variable result is the List with proper Order objects, but the if clause fails and thus return a null value.
So does anyone know how to solve the problem?
===========================================================
To make it more clear, I'll show my db schema:



and here are the sql generated by the Hibernate:
Hibernate: select order0_.id as id1_8_, order0_.address_id as address_6_8_, order0_.date as date2_8_, order0_.deliver_time as deliver_3_8_, order0_.restaurant_id as restaura7_8_, order0_.status as status4_8_, order0_.total as total5_8_, order0_.user_email as user_ema8_8_ from orders order0_ where order0_.user_email=? Hibernate: select address0_.id as id1_1_0_, address0_.location as location2_1_0_, address0_.name as name3_1_0_, address0_.phone as phone4_1_0_, address0_.user_email as user_ema5_1_0_, user1_.email as email1_14_1_, user1_.password as password2_14_1_, user1_.pts as pts3_14_1_, user1_.status as status4_14_1_, user1_.user_name as user_nam5_14_1_ from address address0_ left outer join user user1_ on address0_.user_email=user1_.email where address0_.id=? Hibernate: select restaurant0_.id as id1_9_0_, restaurant0_.email as email2_9_0_, restaurant0_.location as location3_9_0_, restaurant0_.name as name4_9_0_, restaurant0_.password as password5_9_0_, restaurant0_.phone as phone6_9_0_, restaurant0_.status as status7_9_0_, restaurant0_.type as type8_9_0_, restaurant0_.vcode as vcode9_9_0_ from restaurant restaurant0_ where restaurant0_.id=? Hibernate: select address0_.id as id1_1_0_, address0_.location as location2_1_0_, address0_.name as name3_1_0_, address0_.phone as phone4_1_0_, address0_.user_email as user_ema5_1_0_, user1_.email as email1_14_1_, user1_.password as password2_14_1_, user1_.pts as pts3_14_1_, user1_.status as status4_14_1_, user1_.user_name as user_nam5_14_1_ from address address0_ left outer join user user1_ on address0_.user_email=user1_.email where address0_.id=?
Источник: https://stackoverflow.com/questions/548 ... empty-list