-
Notifications
You must be signed in to change notification settings - Fork 22
Guice Persist
FluentJdbc has an extension library to support integration with Guice Persist.
<dependency>
<groupId>org.codejargon</groupId>
<artifactId>fluentjdbc-guice-persist</artifactId>
<version>0.9.2</version>
</dependency>
Note: version should match FluentJdbc's version
It implements modules to support:
- Standalone FluentJdbc - including transaction management
- Seamless integration to Guice Persist with JPA (JpaPersistModule)
Both modules will set / override the ConnectionProvider of FluentJdbc, and bind FluentJdbc and Query instances to Guice container and support transaction management with Guice Persist's @Transactional annotation.
Example of usage after configuration:
@Inject
private Query query;
@Transactional
public void updateSomething() {
query.update(someSqlQuery).....
}
Needs configuration of FluentJdbc (through FluentJdbcBuilder), and a DataSource instance (preferably one based on a pool).
DataSource dataSource = ...
FluentJdbcBuilder builder = new FluentJdbcBuilder()...
StandaloneFluentJdbcModule fluentJdbcModule = new StandaloneFluentJdbcModule(builder, dataSource);
install(fluentJdbcModule);
JpaFluentJdbcModule can be used in conjunction with JpaPersistModule to integrate seamlessly with JPA (including transaction management). The module will override the ConnectionProvider with an own implementation that extracts Connections from the JPA EntityManager.
Extraction of Connections from an EntityManager requires the use of vendor specific API. The modules also needs configuration of FluentJdbc (through FluentJdbcBuilder)
FluentJdbcBuilder fluentJdbcBuilder = new FluentJdbcBuilder()...
JpaConnectionExtractor extractor = ...
JpaFluentJdbcModule fluentJdbcModule = new JpaFluentJdbcModule(fluentJdbcBuilder, extractor);
install(..., jpaPersistModule, fluentJdbcModule, ...)
Examples for JpaConnectionExtractor:
JpaConnectionExtractor extractor = entityManager -> {
return entityManager.unwrap(java.sql.Connection.class);
}
JpaConnectionExtractor extractor = entityManager -> {
Session session = entityManager.unwrap(Session.class);
SessionFactoryImplementor sfi = (SessionFactoryImplementor) session.getSessionFactory();
return sfi.getConnectionProvider().getConnection();
}