0

I have a repository base class as defined below.

@NoRepositoryBean
public interface BaseRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
}
public class BaseRepositoryImpl<T, ID extends Serializable> 
    extends SimpleJpaRepository<T, ID> implements BaseRepository<T, ID> {

    public BaseRepositoryImpl(JpaEntityInformation<T, ?> entityInfo, EntityManager entityMgr) {
        super(entityInfo, entityMgr);
    }

    // ...
}
@Configuration
@EnableJpaRepositories(basePackages = "org.example", 
    repositoryBaseClass = BaseRepositoryImpl.class)
public class BaseConfig {
    // additional JPA Configuration
}

I have defined a business repository class and a query method as seen below.

@Repository
public interface CarRepository extends BaseRepository<Car, Long> {
    @Query("SELECT c FROM Car c Where active = 1")
    List<Car> findAllActiveCars();
}

I have a test class which invokes the findAllActiveCars(). I am getting the expected results. But, that query method is not invoking any of the methods in BaseRepository class. How to customize the return values of the query methods?

s_v_2
  • 31
  • 1
  • 6

1 Answers1

0

You didn't show the methods that you did implement, so it is not clear why they don't get called, but since you want to decrypt entity fields, consider listening to JPAs entity lifecycle events. @PostLoad should be able to do the trick.

https://docs.jboss.org/hibernate/core/4.0/hem/en-US/html/listeners.html

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Thanks Jens. I tried the PostLoad. It is invoked only when the entity loaded from the database for the first time. It won't serve my purpose. I would like to know how to intercept the Query based finders in spring data. Looks like those methods are not handled by repositoryBaseClass. – s_v_2 Oct 31 '19 at 22:10
  • Entities generally only get loaded once per session with JPA. It sounds weird that you would want to change an entity, just because it got loaded again. But anyway, you'd want to look into using your own replacement for `JpaRepositoryFactory` and then use your own `QueryLookupStrategy`. Here is an example to get you started: https://stackoverflow.com/questions/53083047/replacing-deprecated-querydsljparepository-with-querydsljpapredicateexecutor-fai – Jens Schauder Nov 01 '19 at 06:07