Я использую springboot 2.1.2, и у меня возникают проблемы с хранилищем.Вот мои классы сущностей:
@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
}
И мой OrderRepository:
@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {
@Query("select o from Order o where o.user.email = ?1")
List<Order> findAllByUserId(String userId);
List<Order> findAllByUser(User user);
}
Когда я вызываю findAllByUserId или findAllByUser, репозиторий возвращает нулевое значение вместо пустой список , это так странно, поскольку я уверен, что в моей базе данных есть данные.
Я читал другие подобные вопросы, и они, похоже, не помогают.
Я пытаюсь решить проблему с отладчиком и отслеживаю класс AsyncExecutionInterceptor :
@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<Object> 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());
}
}
И я замечаю, что в 13-й строке этого метода переменная приводит кэто список с правильными объектами Order, но предложение if не выполняется и, следовательно, возвращает нулевое значение.
Так кто-нибудь знает, как решить проблему?
================================================================
Для большей ясности я покажу свою схему БД:


и вот sql, сгенерированный 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=?