This library provides HK2 injectors and container lifecycle components for common Hibernate functionality. This feature does not contain the jersey dependencies, nor does it contain any hibernate database adapters. We recommend using jersey 2.11 or later, as there are HK2 injection bugs in previous versions that prevent both session scoped and singleton scoped items from being properly destroyed.
Here are the basic steps necessary to get this feature working in your jersey2 application.
The jersey2-hibernate feature is published to a maven repository on github. You can enable the repository with the following in your pom.xml.
<repositories> ... <repository> <id>jersey2-toolkit</id> <url>https://krotscheck.github.com/jersey2-toolkit/repo</url> </repository> ... </repositories> <dependencies> ... <dependency> <groupId>net.krotscheck.jersey2</groupId> <artifactId>jersey2-hibernate</artifactId> <version>1.1.6</version> </dependency> ... </dependencies> import net.krotscheck.jersey2.hibernate.HibernateFeature; import org.glassfish.jersey.server.ResourceConfig; public class YourApplication extends ResourceConfig { public YourApplication() { register(HibernateFeature.class); } }
The configuration of hibernate is beyond the scope of this document, we recommend you read the sections on hibernate configuration for both /src/main/resources/hibernate.properties and /src/main/resources/hibernate.cfg.xml.
At this point, hibernate components are available to inject into your jersey2 services. Here is an example service.
@Path("/path") public class MyService { @Inject public Session session; @Inject public FullTextSession ftSession; @Inject public SessionFactory sessionFactory; @GET @Produces(MediaType.APPLICATION_JSON) public Response getMethod() { return Response.ok().build(); } }