ASA
ASA

Reputation: 1971

How to combine Spring Data JPA QueryBuilder methods and concrete classes + DI?

In Spring Data JPA you can extend the interface JpaRepository or its relatives with your own interface and add query executing methods that are automatically implemented by Spring based on their name.

I often find myself wanting to combine that kind of interface with a concrete class. For example, I want my Repository to offer a method to create and initially populate new entities with the help of other dependencies, which is currently quite cumbersome since there is no pleasant way to inject dependencies in Entity classes.

Is that possible and if so, how?

Upvotes: 1

Views: 1339

Answers (1)

Oliver Drotbohm
Oliver Drotbohm

Reputation: 83081

There's a dedicated section in the reference documentation on how to add custom code to Spring Data repositories.

However I'd advise not to inject things into entities as you basically create a cyclic relationship between repositories, services and the entities they work with.

Upvotes: 1

Related Questions