Persist Object using Hibernate and Guice
<p>I want to persist an object in my database, I'm using @Trasactional and @Inject from Guice to persist the data .... the hibernate configuration is declared in a file persistence.xml in ressources/meta-inf folder .... </p>

<pre><code>import ca.model.jpa.user;
import com.google.inject.Inject;
import com.google.inject.persist.Transactional;

import javax.persistence.EntityManager;
import javax.ws.rs.*;


@Path("/test")
public class TestService {

    @Inject
    private EntityManager entityManager;

    @GET
    @Transactional
    public String persistUser() {

        // User
        User user = new User();
        user.setPseudoname("Anonymous");
        user.setAge(20);

        entityManager.persist(user);

        return null;
    }
</code></pre>

<p>when I debug my code, the entityManager is always null ... In my understanding, the injection aim to initiate the entityManager. But it doesn't do so !</p>

<p>How can I correct My code ? thank you</p>

<hr>

<p>Here the content of my file persistence.xml :</p>

<pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0"&gt;

    &lt;persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL"&gt;
        &lt;provider&gt;org.hibernate.ejb.HibernatePersistence&lt;/provider&gt;

        &lt;properties&gt;

            &lt;!--PostgreSQL--&gt;
            &lt;property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/&gt;
            &lt;property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/&gt;
            &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt;
            &lt;property name="hibernate.connection.username" value="postgres"/&gt;
            &lt;property name="hibernate.connection.password" value="postgres"/&gt;
            &lt;property name="hibernate.archive.autodetection" value="class"/&gt;
            &lt;property name="hibernate.id.new_generator_mappings" value="true"/&gt;
            &lt;property name="hibernate.hbm2ddl.auto" value="update"/&gt;
            &lt;property name="hibernate.ejb.metamodel.generation" value="disabled"/&gt;
            &lt;property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultComponentSafeNamingStrategy"/&gt;
            &lt;property name="hibernate.show_sql" value="true"/&gt;
        &lt;/properties&gt;

    &lt;/persistence-unit&gt;

&lt;/persistence&gt;
</code></pre>