Problem with JPA

Asked

Viewed 142 times

1

Classes and persistence.xml below. The problem is: entityManager is Coming NULL.

 @ManagedBean(name="pessoaController")
    @SessionScoped
    public class PessoaController{
    private List<Pessoa> pessoas = new ArrayList<Pessoa>();

    public PessoaController() {
        new PessoaHome().findById(1);
    }

    public List<Pessoa> getPessoas(){
        return pessoas;
    }
    public void setPessoas(List<Pessoa> pessoas){
        this.pessoas = pessoas;
    }

}    


@Stateless
public class PessoaHome {

private static final Log log = LogFactory.getLog(PessoaHome.class);

@PersistenceContext
private EntityManager entityManager;

public void persist(Pessoa transientInstance) {
    log.debug("persisting Pessoa instance");
    try {
        entityManager.persist(transientInstance);
        log.debug("persist successful");
    } catch (RuntimeException re) {
        log.error("persist failed", re);
        throw re;
    }
}

public void remove(Pessoa persistentInstance) {
    log.debug("removing Pessoa instance");
    try {
        entityManager.remove(persistentInstance);
        log.debug("remove successful");
    } catch (RuntimeException re) {
        log.error("remove failed", re);
        throw re;
    }
}

public Pessoa merge(Pessoa detachedInstance) {
    log.debug("merging Pessoa instance");
    try {
        Pessoa result = entityManager.merge(detachedInstance);
        log.debug("merge successful");
        return result;
    } catch (RuntimeException re) {
        log.error("merge failed", re);
        throw re;
    }
}

public Pessoa findById(int id) {
    log.debug("getting Pessoa instance with id: " + id);
    try {
        Pessoa instance = entityManager.find(Pessoa.class, id);
        log.debug("get successful");
        return instance;
    } catch (RuntimeException re) {
        log.error("get failed", re);
        throw re;
    }
}



<persistence-unit name="postgresDS" transaction-type="JTA">
    <jta-data-source>java:/postgresDS</jta-data-source>     

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>      
        <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>

        <property name="hibernate.show_sql" value="false"/>
        <property name="hibernate.format_sql" value="false"/>

        <property name="hibernate.hbm2ddl.auto" value="none" />
    </properties> 

</persistence-unit>  

  • 1

    Could you translate your title and question to English?

  • 1

    Please next time do not post in English.

  • Which application server are you deploying to? Post your datasource to us (postgresDS). One thing I noticed is that your datasource has the same name as your PU.

  • @Ricardogiaviti also noticed this detail, but I’m not sure if this is the mistake. Felipe, you already tried to change the name of your PU to something else?

  • As @Ricardogiaviti said, what is your application server? Another thing, you put the Datasource reference on the web.xml?

1 answer

1

Add unitName in the annotation PersistenceContext.

Thus remaining:

//...
@PersistenceContext(unitName = "postgresDS")
private EntityManager entityManager;
//...
  • If persistence.context has only one declaration, by default, it is not necessary to add the name. He couldn’t even call the project if that was the reason.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.