Who's responsible for instantiating the TestEndpoint class? Is it really 
instantiated by Guice or is Guice only injecting its members (in which case 
AOP won't work) ?

On Friday, October 18, 2013 11:06:53 AM UTC+2, Dirk Vranckaert wrote:
>
> I'm building a backend application for one of my Android applications.
> My setup is Hibernate as ORM, MySQL as DB, Guice as DI and Guice-Persist 
> for the link between ORM and DI.
>
> My webservice-endpoint class looks like this:
>
> @Path("test")
> public class TestEndpoint {
>     @Inject
>     private UserDao userDao;
>
>     @GET
>     @Path("persistUser")
>     @Produces(MediaType.TEXT_PLAIN)
>     @Transactional
>     public String persistUser() {
>         User user = new User();
>         user.setEmail("[email protected] <javascript:>");
>         user.setFirstName("FirstName");
>         user.setLastName("LastName");
>         user = userDao.persist(user);
>         return "ok";
>     }
> }
>
> When I manually manage the transaction it works and my entity is persisted:
>
> @Path("test")
> public class TestEndpoint {
>     @Inject
>     private UserDao userDao;
>
>     @Inject
>     private Provider<EntityManager> em;
>
>     @GET
>     @Path("persistUser")
>     @Produces(MediaType.TEXT_PLAIN)
>     public String persistUser() {
>         
> ((HibernateEntityManager)em.get()).getSession().getTransaction().begin();
>
>         User user = new User();
>         user.setEmail("[email protected] <javascript:>");
>         user.setFirstName("FirstName");
>         user.setLastName("LastName");
>         user = userDao.persist(user);
>
>         
> ((HibernateEntityManager)em.get()).getSession().getTransaction().commit();
>
>         return "ok";
>     }
> }
>
> My UseDaoImpl that is injected via UserDao looks like this:
>
> public abstract class UserDaoImpl {
>     @Inject
>     private Provider<EntityManager> em;
>
>     public EntityManager getEntityManager() {
>         return em.get();
>     }
>     public User persist(User instance) {
>         getEntityManager().persist(instance);
>         return instance;
>     }
>
>     public User findById(String id) {
>         return getEntityManager().find(User.class, id);
>     }
> }
>
> My GuiceContextListener looks like this:
> public class GuiceContextListener extends GuiceServletContextListener {
>     @Override
>     protected Injector getInjector() {
>         Injector injector = Guice.createInjector(
>                 new DatabaseModule(),
>                 new GuiceDaoModule(),
>                 new GuiceServiceModule(),
>                 new ServletModule()
>         );
>         return injector;
>     }
> }
>
> The DatabaseModule:
> public class DatabaseModule extends AbstractModule {
>     private static final String JPA_UNIT = "myDB";
>
>     @Override
>     protected void configure() {
>         install(new JpaPersistModule(JPA_UNIT));
>     }
> }
>
> The GuiceDaoModule:
> public class GuiceDaoModule implements Module {
>     @Override
>     public void configure(Binder binder) {
>         binder.bind(UserDao.class).to(UserDaoImpl.class);
>     }
> }
>
> And this is my web.xml configuration:
> <?xml version="1.0" encoding="ISO-8859-1"?>
> <web-app xmlns="http://java.sun.com/xml/ns/javaee";
>          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
>          xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
> http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd";
>          version="2.5">
>   <display-name>MyApp</display-name>
>
>     <listener>
>         
> <listener-class>eu.vranckaert.test.guice.GuiceContextListener</listener-class>
>     </listener>
>
>     <!-- GUICE -->
>     <filter>
>         <filter-name>guiceFilter</filter-name>
>         <filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
>     </filter>
>
>     <filter-mapping>
>         <filter-name>guiceFilter</filter-name>
>         <url-pattern>/*</url-pattern>
>     </filter-mapping>
> </web-app>
>
>
> When I do findById(..) I can also retrieve my record, so my DB connection 
> is working, I'm just not able to use the @Transactional annotation... 
> So I found something about wrong scope of my EntityManager, but I have no 
> clue how I can change the entity manager's scope...
>
> You guys have any idea what is wrong with my setup that the annotation 
> does not work?
>
> Dirk
>

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to