Hi Martin,
A number of developers on the list have experience with Jersey+Hibernate
but i think you may be the first to play with combining the JPA and
resources into one class.
Martin Probst wrote:
> Hi all,
>
> I'm currently trying to integrate Hibernate and Jersey, which gives me
> some trouble with the injection of context stuff.
>
> I'm trying to make my Hibernate (JPA) persistent domain model act as
> Jersey resources at the same time, so I have classes like this:
>
> @Entity
> @Path("/domain")
> public class DomainA {
> @Path("{domainBId}")
> public getDomainBById(long id) {
> return entityManager.find(DomainB.class, id); // needs injecting
> }
> @POST
> public createDomainB() {
> DomainB db = new DomainB(); // needs injecting
> }
> }
>
> Now the trouble is that the domain objects need some context from
> Jersey, for example the UriInfo to create links to them.
>
> public class DomainB {
> @Context UriInfo info; // needs injecting
> }
>
> But the domain classes are instantiated by Hibernate, so injection
> doesn't happen. There are several different methods of tapping into
> Hibernates object instantiation, for example the class
> Instantiator.instantiate() methods (though I still don't quite know how
> to register my instantiator with Hibernate).
>
> My problem is that I have to get hold of Jersey's ComponentProvider, and
> I see no obvious way of getting to it.
You can supply your own component provider if you extend:
com.sun.ws.rest.spi.container.servlet.ServletContainer
as we are doing for the spring integration. See the initial servlet i
blogged about [1], or look in the spring-integration branch [2]. Jersey
will wrap a supplied component provider and defer to that (if it returns
null jersey will try and instantiate directly in a kind of keep in
trucking approach).
Note that ComponentProvider is used to instantiate all components,
resource classes or otherwise (e.g. MessageBodyReader/Writer etc) so if
your provider is defering to some entity manager you need to be careful
to check for @Entity otherwise to return null and Jersey will instantiate.
> I'd also need that one to
> properly set up a new instance of my DomainB.
>
Martin G. has implemented a class called ResourceContext that can be
injected and enables one to get an instance of a resource class (hiding
all the details about life-cycle and use of the component provider). If
you can break the chicken or egg problem with your own component
provider i think it should work. But this is currently in the
spring-integration branch. We are working to integrate this into the trunk.
> Is there any documentation, or did anybody already try this?
>
I try and keep JavaDoc up to date but... the best documentation at the
moment is asking this list :-)
Paul.
[1]
http://blogs.sun.com/sandoz/entry/integrating_jersey_and_spring_take
[2]
https://jersey.dev.java.net/source/browse/*checkout*/jersey/branches/spring-integration/spring/src/main/java/com/sun/ws/rest/spring/SpringServlet.java
> Thanks,
> Martin
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
--
| ? + ? = To question
----------------\
Paul Sandoz
x38109
+33-4-76188109