I have set of classes which inherit from a single super class:
Super
|
+------+-------+
Aaaa Bbbb Cccc
Each of the Aaaa,Bbbb,Cccc then should contain method findByTag. The problem is that I can’t manage to define it generally. Following example defines specific findByTag for Aaaa.
public interface AaaaRepository extends SuperRepository<Aaaa> {
@Query("select distinct a from Aaaa a " +
"join a.tags t " +
"join fetch a.locale where t = ?1")
public List<Event> findByTag(Tag t);
}
Note that the Superclass is @MappedSuperclass and does not have its own table in database.
I would like to use some kind of “Super” in the query which would be replaced in each class by its name.
My second problem is that I don’t know how to force @ElementCollection to be Eagerly fetched. I have to always explicitly say “join fetch” in the query. If it is not fetched, once the transaction is finished, I can’t access those objects, which I did not explicitly fetched. (LazyFetch Exceptions…)
Thanks
Looking at the documentation, custom implementations section, what about this approach:
Read the documentation to use this implementation as a base class for the repository factory, then Spring Data will build implementations for the other repositories based on this custom one.
Look at “Example 1.16. Custom repository factory bean” of the documentation to create the factory bean.
When Spring instantiates the implementation of AaaaRepository, it will use MyRepositoryImpl as base class.
Will this work for you?