Databases Reference
In-Depth Information
At the moment we are perfectly happy with the default suffix. Thus, the name of the
class that implements our custom interface must be ContactRepositoryImpl . We
can implement this class by following these steps:
1. Write some plumbing code needed to configure the
QueryDslJpaRepository<T, ID> class that is used to execute our queries.
2.
Implement the methods declared in our custom interface.
Configuring the repository class
In this phase we will write the code that is needed to obtain an instance of
the QueryDslJpaRepository<Contact, Long> class. This process has the
following steps:
1.
Use the @PersistenceContext annotation to get a reference to the used
entity manager.
2.
Create an init() method and annotate it with the @PostConstruct
annotation. This ensures that the method called after the bean has been
constructed and the entity manager reference is injected.
3.
Implement the init() method and create a new
QueryDslJpaRepository<Contact, Long> object.
The source code of our implementation is given as follows:
public class ContactRepositoryImpl implements
PaginatingContactRepository {
@PersistenceContext
private EntityManager entityManager;
private QueryDslJpaRepository<Contact, Long> repository;
//Add methods here
@PostConstruct
public void init() {
JpaEntityInformation<Contact, Long> contactEntityInfo =
new JpaMetamodelEntityInformation<Contact, Long>(Contact.class,
entityManager.getMetamodel());
repository = new QueryDslJpaRepository<Contact,
Long>(contactEntityInfo, entityManager);
}
}
 
Search WWH ::




Custom Search