users@jersey.java.net

Integrating Jersey & Hibernate

From: Martin Probst <mail_at_martin-probst.com>
Date: Mon, 21 Apr 2008 09:49:44 +0200

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. I'd also need that one to
properly set up a new instance of my DomainB.

Is there any documentation, or did anybody already try this?

Thanks,
Martin