Skip to content

Support AggregateReference in derived query methods #987

@yangwansu

Description

@yangwansu

I created a query method using a Reference from another Aggregate.

@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);
}

But I got the following exception:

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

I have checked the source below. But I don't understand why it throws an exception.

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()));
    }
}

Why can't I create a query method on a Reference?

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions