-
Notifications
You must be signed in to change notification settings - Fork 24
JPA Support
JPA is supported out of the box with Hammock, allowing you to leverage the built in functions as well as some custom grown features.
Before we get into JPA, its important to understand how configuration of DataSources works. There are a few ways to create DataSources using Hammock. Hammock does provide support for @DataSourceDefinition
and @DataSourceDefinitions
found on any CDI managed bean. However, a producer method is probably the cleaner way since it allows the configuration to be externalized. Such a bean may look like this:
@Inject
@ConfigProperty(name="test2.url")
private String url;
@Inject
@ConfigProperty(name="test2.username")
private String username;
@Inject
@ConfigProperty(name = "test2.password")
private String password;
@Produces
@ApplicationScoped
@Named("test2")
public DataSource createDataSource(DataSourceDefinitionBuilder builder) {
return builder.url(url)
.user(username)
.password(password)
.name("test2")
.build();
}
Hammock by default creates a default persistence unit for your app, and unless you need something special its probably the right way to go. The unit is named __default
and it works by finding all entities defined within your application, pointing to the datasource defined by the name in hammock.jpa.__default.datasource
. All custom properties that are prefixed by hammock.jpa.__default
will get added to the EntityManagerFactory
on creation, allowing you to override additional configuration via properties.