Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jiakuan/wise-persist

A simple JPA wrapper which provides @Transactional and @NonTransactional annotations and pre-configured Guice module.
https://github.com/jiakuan/wise-persist

Last synced: 15 days ago
JSON representation

A simple JPA wrapper which provides @Transactional and @NonTransactional annotations and pre-configured Guice module.

Awesome Lists containing this project

README

        

## WisePersist

WisePersist is a simple JPA wrapper which provides `@Transactional` and `@NonTransactional` annotations and pre-configured
Guice module.

### Configure Maven dependency

WisePersist is available in Maven central repository:

http://search.maven.org/#search%7Cga%7C1%7Cwise-persist

To add the dependency to your `build.gradle` (for Gradle projects) or `pom.xml` (for Maven projects), use the following configuration:

For Gradle projects:

```
compile 'org.wisepersist:wise-persist:1.0.4'
```

For Maven projects:

```

org.wisepersist
wise-persist
1.0.4

```

If you would like to use the 1.0.5-SNAPSHOT release, use this configuration.

```

org.wisepersist
wise-persist
1.0.5-SNAPSHOT

```

In order to use snapshot releases you also need to add the Sonatype snapshots repository to your POM:

```


sonatype-nexus-snapshots
http://oss.sonatype.org/content/repositories/snapshots

true


false

```

### How to use WisePersist?

In your JPA projects, mark any public methods which are expected to be transactional with `@Transactional` annotation or `@NonTransactional` for non transactional methods. Please note that all public methods in your DAO classes must be annotated by either `@Transactional` or `@NonTransactional`, or you will get exception mentioning this.

For example:

```
import com.google.common.base.Optional;

import java.util.List;

import org.wisepersist.AbstractDao;
import org.wisepersist.NonTransactional;
import org.wisepersist.Transactional;

import javax.persistence.Query;
import javax.persistence.TypedQuery;

/**
* @author jiakuanwang
*/
public class UserDao extends AbstractDao {

@Transactional
public User saveUser(User user) {
return em().merge(user);
}

@NonTransactional
public User Optional findById(Long userId) {
TypedQuery query =
em().createQuery("SELECT u FROM User u WHERE u=:userId", DataUser.class);
query.setParameter("userId", userId);

List userList = query.getResultList();
if (userList != null && userList.size() > 0) {
User user = userList.get(0);
return Optional.of(user);
}
return Optional.absent();
}
}
```

In the example above, we need to extend the AbstractDao class for every DAO class firstly.

In public DAO methods, we could just use `em()` to get the current available entity manager instance.

Each method with `@Transactional` annotation will start a new transaction and commit/rollback the transaction automatically. Please note that transactional methods cannot be nested. That is, in the example above, inside the `saveUser` method mentioned above should not call any other methods annotated with `@Transactional` or `@NonTransactional`.

After that, you can use this DAO with Guice injector. For example:

```
/**
* @author jiakuanwang
*/
public class UserDaoTest {

private final Injector injector = Guice.createInjector(
new WisePersistModule("WTPersistUnitH2")
);
private final UserDao userDao = injector.getInstance(UserDao.class);

@Test
public void testSaveUser() throws Exception {
User user = new User();
user.setEmail("[email protected]");
user.setFirstName("Jake");
user.setLastName("Wang");
userDao.saveUser(user);
}
}
```

As shown in the code above, the first step is to create `WisePersistModule` with persist unit name (or you could pass a custom entity manager factory) and put in the Guice module list when creating the Guice injector, and then get a `UserDao` instance from the Guice injector, now you are ready to go!

With this simple tiny framework, we don't need to manually begin and close transactions again and again.