为什么我不能通过引用查询Spring Data Jdbc?



我使用另一个Aggregate中的Reference创建了一个查询方法。

@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor_=@PersistenceConstructor)
public static class Book {
public static Book of(Author author) {
return new Book(null, AggregateReference.to(author.id));
}
@Id
private Long id;
private AggregateReference<Author, Long>  authorId;
}
interface BookRepository extends CrudRepository<Book, Long> {
List<Book> findBooksByAuthorId(Long  authorId);
}

但我得到了以下例外:

Caused by: java.lang.IllegalArgumentException: Cannot query by reference: authorId
at 

我已经检查了下面的来源。但我不明白为什么它会抛出一个例外。

org.springframework.data.jdbc.repository.query.JdbcQueryCreator.validateProperty(JdbcQueryCreator.java:146)
private static void validateProperty(PersistentPropertyPathExtension path) {
.....
if (path.getRequiredPersistentPropertyPath().getLeafProperty().isReference()) {
throw new IllegalArgumentException(
String.format("Cannot query by reference: %s", path.getRequiredPersistentPropertyPath().toDotPath()));
}
}

为什么我不能在引用上创建查询方法?

供您参考的来源在这里。https://github.com/yangwansu/try-spring-data-jdbc/blob/main/src/test/java/masil/example/springdata/jdbc/ch9_7/QueryToAggregateReferenceTest.java

对于以后来这里的任何人:

这是Spring Data JDBC的一个限制。谢谢你找到它。

它现在得到了适当的实施。包含修复程序的第一个版本是2.3M1版本。

有关详细信息,请参阅杨万素创建的问题。

最新更新