问题描述
@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.3 M1 版本。
有关详细信息,请参阅 the issue created by Wansu yang。