Search code examples
springhibernatewicketspring-ormwicket-1.6

Accessing DB via JPA in Wicket IResource?


I want to implement an DynamicImageResource which will be mounted in a Wicket 6.7/Spring 3.2/JPA 2 application. This DynamicImageResource shall retrieve information based on input parameters and create a response.

The question is: How can i access the DB from within DynamicImageResource.getImageData(...)? I can't inject a @PersistenceContextenter code here or a DAO via @SpringBean. My last resort would be a static PersistenceContextFactory.

Any better ideas? Or best practices?


Solution

  • As Wicket manages component instanciation for you, or you manually instanciate objects via their respective constructors, injection mechanisms couldn't work out of the box (the object are, per manual constructor invocation, unmanaged by your container).

    Wicket provides built-in injection for Component via inheritance, the magic behind is Wicket calling componentInstanciationListeners. For every unmanaged class not inherited from an injection enabled one, you have to manually trigger the injection on your bean.

    As stated in the comment, you should call Injector.get().inject(this); in your constructor in order for it to work.