Export to GitHub

guice-repository - DevGuide.wiki


Developer Guide

Welcome to the developer documentation of guice-repository project - a Google Guice adapted version of Spring Data-JPA project with some additional features.

Current project version is: 2.1.0

What are Spring Data-JPA?

Here is description from Spring Data-JPA authors:

"Spring JPA is part of the umbrella Spring Data project that makes it easy to easily implement JPA based repositories."

For my opinion it is very useful project that helps developer to work hard on business logic and avoid writing boilerplate code around persistence layer.

What are guice-repository?

Guice-repository is an adapter for Spring Data-JPA project which brings all known functionality of Guice-Persist with many additional features: * Support for batch store (see this page); * Allow access to EntityManager from Repository (see EntityManagerProvider); * Full support for @Transactional methods for all Guice-instantiated entities (see this page); * Support for multiple persistence units; * Support for direct injection of EntityManager/EntityManagerFactory with @PersistenceContext/@PersistenceUnit/@Inject&@Named annotations; * Support for injections in repositories/custom implementations; * PersistFilter as implementation of "Open EntityManager in View"/"session-in-view"/"session-per-http-request" pattern; * Offers natural way of Repository binding process; * Repository auto-bind possibilities with exclusion/inclusion filters - see AutoBind; * Ability to bind interceptor to catch Repository methods with @Transactional.

NOTE: guice-repository is not compatible with guice-persist extension and can't coexist in one pom.xml.

How did this work?

There is a three main steps:

  1. Define a Repository interface

``` public interface AccountRepository extends JpaRepository, EntityManagerProvider {

Account findAccountByUuid(String uuid);

@Query("select a from Account a where a.name = :name")
Account findAccountByName(@Param("name") String name);

} ```

This process is well described in the original manual. All features described in 1.1-1.4 documentation parts is supported by guice-repository.

  1. Install a Guice-module

You can select between manual Repository binding in JpaRepositoryModule and auto-binding with ScanningJpaRepositoryModule.

Example for JpaRepositoryModule: install(new JpaRepositoryModule("my-persistence-unit") { @Override protected void bindRepositories(RepositoryBinder binder) { binder.bind(AccountRepository.class).to("my-persistence-unit"); } });

Example for ScanningJpaRepositoryModule: install(new ScanningJpaRepositoryModule("com.mycorp.repo", "my-persistence-unit"));

In this case "com.mycorp.repo" is a package where your Repository interfaces is located.

  1. @Inject & use

Inject and use Repository in your services/business-logic modules:

``` public class AccountService {

@Inject
private AccountRepository accountRepository;

public void registerUser(String login, String password) throws RegistrationException{
 // ... some checks & etc
 accountRepository.save(new Account(login, password));
 // ... something else
}

public Account findAccount(String login) throws FinderException{
 return accountRepository.findAccountByLogin(login);
}

} ```

Why not just use pure Guice-Persist?

My own best-reasons-list for this is: * Reduce boilerplate code around persistence layer * Simplify understanding of Domain model and its lifecycle aspects between team members * Isolation from ORM-configuration vendor specifics (see this page) in EntityManager configuration * OOM-protected Batch-insert feature

Where i can get it?

Project is mavenized and published in Central Maven repository.

<dependency> <groupId>com.google.code.guice-repository</groupId> <artifactId>guice-repository</artifactId> <version>${version.guice-repository}</version> </dependency>

This project is depends on many other 3rd party libraries which is required to work. So, without Maven you should manually add actual versions of this 3rd party libraries. Actual list of them can be viewed in dependencies section of pom.xml.

Actual project version will be always on top of this page.