Last active
August 29, 2015 13:57
-
-
Save Sch3lp/9764767 to your computer and use it in GitHub Desktop.
dropwizard-hibernate issue: our setup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CustomerRepository extends AbstractDAO<Customer> { | |
public CustomerRepository(SessionFactory sessionFactory) { | |
super(sessionFactory); | |
} | |
public List<Customer> getAll() { | |
return list(namedQuery(Customer.QUERY_ALL)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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