Skip to content

Instantly share code, notes, and snippets.

@Sch3lp
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sch3lp/9764767 to your computer and use it in GitHub Desktop.
Save Sch3lp/9764767 to your computer and use it in GitHub Desktop.
dropwizard-hibernate issue: our setup
public class CustomerRepository extends AbstractDAO<Customer> {
public CustomerRepository(SessionFactory sessionFactory) {
super(sessionFactory);
}
public List<Customer> getAll() {
return list(namedQuery(Customer.QUERY_ALL));
}
}
@Path("/v1/customers")
@Produces(value = MediaType.APPLICATION_JSON)
@Consumes(value = MediaType.APPLICATION_JSON)
@Api(value = "/v1/customers", description = "Operations to manage Customers")
public class CustomerResourceV1 {
private CustomerRepository customerRepository;
private CustomerTransformer customerTransformer;
public CustomerResourceV1(CustomerRepository customerRepository, CustomerTransformer customerTransformer) {
this.customerRepository = customerRepository;
this.customerTransformer = customerTransformer;
}
@GET
@Timed
@UnitOfWork(readOnly = true)
public List<CustomerR> getAllCustomers() {
return customerTransformer.toRepresentations(customerRepository.getAllCustomers());
}
}
db:
# the name of your JDBC driver
driverClass: com.mysql.jdbc.Driver
# the username
user: herp
# the password
password: derp
# the JDBC URL
url: jdbc:mysql://localhost:3306/local_db
# any properties specific to your JDBC driver:
properties:
charSet: UTF-8
# the maximum amount of time to wait on an empty pool before throwing an exception
maxWaitForConnection: 1s
# the SQL query to run when validating a connection's liveness
validationQuery: "/* AssetsService Health Check */ SELECT 1"
# the minimum number of connections to keep open
minSize: 8
# the maximum number of connections to keep open
maxSize: 32
# whether or not idle connections should be validated
checkConnectionWhileIdle: false
# how long a connection must be held before it can be validated
checkConnectionHealthWhenIdleFor: 10s
# the maximum lifetime of an idle connection
closeConnectionIfIdleFor: 1 minute
public class AssetsService extends Service<AssetsConfiguration> {
public static ImmutableList<Class<?>> hibernentities = ImmutableList.<Class<?>> of(Customer.class, Location.class);
public static ImmutableList<Class<?>> authentities = ImmutableList.<Class<?>> of(AuthenticatedUser.class);
private final HibernateBundle<AssetsConfiguration> hibernate = new HibernateBundle<AssetsConfiguration>(hibernentities,
new SessionFactoryFactory()) {
@Override
public DatabaseConfiguration getDatabaseConfiguration(AssetsConfiguration configuration) {
return configuration.getDatabaseConfiguration();
}
};
private final HibernateBundle<AssetsConfiguration> authHibernate = new HibernateBundle<AssetsConfiguration>(authentities,
new SessionFactoryFactory()) {
@Override
public DatabaseConfiguration getDatabaseConfiguration(AssetsConfiguration configuration) {
return configuration.getAuthManagerDbConfiguration();
}
};
@Override
public void initialize(Bootstrap<AssetsConfiguration> bootstrap) {
bootstrap.setName("Assets");
bootstrap.addBundle(hibernate);
bootstrap.addBundle(authHibernate);
}
@Override
public void run(AssetsConfiguration configuration, Environment environment) throws Exception {
CustomerRepository customerRepository = new CustomerRepository(hibernate.getSessionFactory());
CustomerTransformer customerTransformer = new CustomerTransformer();
environment.addResource(new CustomerResourceV1(customerRepository, customerTransformer));
replaceDropWizardInvalidEntityExceptionMapperWithOurOwn(environment);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment