Problem with JPA using Wildfly 9 and JTA

Asked

Viewed 1,233 times

0

It’s the first time I’ve tried to use JTA using the Wildfly container and so far I haven’t been able to execute my code properly. When I publish the application(start Wildfly) it starta perfectly, generates the table and such. But when I enter the screen to display a simple list an error occurs.

Follow below the screenshot of the error screen: inserir a descrição da imagem aqui

I already set the datasource in wildfly (so much so that it generates the table in the host database).

I’m using the dao pattern in the project.

My persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="UmariPU" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/UmariDS</jta-data-source>
    <class>br.com.umari.entities.Estabelecimento</class>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />
            <property name="hibernate.jdbc.wrap_result_sets" value="false" />
            <property name="hibernate.hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>  

My generic dao:

package br.com.umari.dao.impl;

import java.lang.reflect.ParameterizedType;
import java.util.Collection;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.umari.dao.GenericDAO;

@SuppressWarnings("unchecked")
public class GenericDAOImpl<T, PK> implements GenericDAO<T, PK> {

    @PersistenceContext(unitName = "UmariPU")
    protected EntityManager entityManager;



    public void persist(T entity) {
        System.out.println("Entrou em genericDAO");
        entityManager.persist(entity);
    }

    public void marge(T entity) {
        entityManager.merge(entity);

    }

    public void remove(T entity) {
        entityManager.remove(entity);
    }

    public void removeById(PK id) {
        T entity = getByID(id);
        entityManager.remove(entity);
    }

    public T getByID(PK id) {
        return (T) entityManager.find(getTypeClass(), id);
    }

    public Collection<T> findAll() {
        return entityManager.createQuery("FROM" + getTypeClass().getName()).getResultList();
    }

    public Query createQuery(String query, Object... parameters) {
        Query q = entityManager.createQuery(query);

        for (int i = 1; i < parameters.length; i++) {
            q.setParameter(i, parameters[i]);
        }
        return q;
    }

    private Class<?> getTypeClass() {
        Class<?> clazz = (Class<?>) ((ParameterizedType) this.getClass().getGenericSuperclass())
                .getActualTypeArguments()[1];
        return clazz;
    }

}

I am using the following dependencies(pom.xml):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>br.com.umari.erp</groupId>
    <artifactId>Umari</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>javax.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.1</version>
        </dependency>
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>5.3</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.5.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.5.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-annotations</artifactId>
            <version>3.5.6-Final</version>
        </dependency>

        <dependency>
            <groupId>org.primefaces.extensions</groupId>
            <artifactId>all-themes</artifactId>
            <version>1.0.8</version>
        </dependency>
        <dependency>
            <groupId>javax.ejb</groupId>
            <artifactId>javax.ejb-api</artifactId>
            <version>3.2</version>
        </dependency>
        <dependency>
            <groupId>javax.inject</groupId>
            <artifactId>javax.inject</artifactId>
            <version>1</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>


        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>1.1.0.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

    </dependencies>
</project>

The class Belecimentomb

package br.com.umari.managedbeans;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.RowEditEvent;

import br.com.umari.ejbs.EstabelecimentoEjb;
import br.com.umari.entities.Estabelecimento;
import br.com.umari.util.UtilErros;
import br.com.umari.util.UtilMensagens;

@Named
@RequestScoped
public class EstabelecimentoMB implements Serializable{
    private static final long serialVersionUID = 2941386880795673862L;

    @Inject
    private EstabelecimentoEjb bean;

    private Estabelecimento estabelecimento;

    private List<Estabelecimento> estabelecimentos;

    @PostConstruct
    private void init(){
        estabelecimentos = (List<Estabelecimento>) bean.findAll();
        estabelecimento = new Estabelecimento();
    }

    public String cadastrar(){
        try {
            bean.persist(estabelecimento);
            estabelecimentos = (List<Estabelecimento>) bean.findAll();
        } catch (Exception e) {

        }

        return "";
    }

    public String atualizar(){
        try{
            bean.merge(estabelecimento);
        }catch(Exception e){
            UtilMensagens.mensagemErro(UtilErros.getMensagemErro(e));
        }

        return "";
    }

    public void excluir(){
        try {
            bean.remove(estabelecimento);
            estabelecimentos = (List<Estabelecimento>) bean.findAll();
        } catch (Exception e) {
            UtilMensagens.mensagemErro(UtilErros.getMensagemErro(e));
        }
    }

    public void onEdit(RowEditEvent event){
        estabelecimento = (Estabelecimento) event.getObject();
        atualizar();
        UtilMensagens.mensagemInformacao("Estabelecimento " + estabelecimento.getRazaoSocial() + " atualizado!");
    }

    public Estabelecimento getEstabelecimento() {
        return estabelecimento;
    }

    public void setEstabelecimento(Estabelecimento estabelecimento) {
        this.estabelecimento = estabelecimento;
    }

    public List<Estabelecimento> getEstabelecimentos() {
        return estabelecimentos;
    }

    public void setEstabelecimentos(List<Estabelecimento> estabelecimentos) {
        this.estabelecimentos = estabelecimentos;
    }




}

Debug screen: inserir a descrição da imagem aqui

Code available in the repository: https://bitbucket.org/umari_tecnologia/erp I am very grateful to those who can give me a light...

  • Can you post the Establishment class?

  • yes. Just a moment.

  • edited the question.

1 answer

1

You are trying to make use of the init() method that is set as private, try changing to the access modifier public.

In the class this being used annotations of two specifications, CDI and JSF, to avoid conflicts it is interesting to use annotations either only from JSF or only from CDI.

In your Establishment, with @Named from CDI and @Requestscoped from JSF

To use CDI only, use

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

JSF

import javax.faces.bean.RequestScoped;
import javax.faces.bean.ManagedBean;

I will leave some remarks by this using JPA

In the dependencies is using Hibernate 5.0, in this case also recommend using the JPA 2.1, in its persistence.xml is with JPA version 2.0

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">
    <persistence-unit name="UmariPU" transaction-type="JTA">

        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:/UmariDS</jta-data-source>

        <class>br.com.umari.entities.Estabelecimento</class>
        <properties>
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="false" />
            <property name="hibernate.use_sql_comments" value="false" />
            <property name="hibernate.jdbc.wrap_result_sets" value="false" />
            <property name="hibernate.hibernate.cache.use_query_cache" value="true" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>
</persistence>

Taking advantage, also start making use of the <provider> HibernatePersistenceProvider package org.hibernate.jpa.

The package persistence Hibernate org.hibernate.ejb was deprecated in these latest versions of Hibernate.

If you still have a problem with findAll, try the API Criteria.

public List<T> findAll() {
    CriteriaBuilder criteriaBuilder = manager.getCriteriaBuilder();
    CriteriaQuery<T> query = criteriaBuilder.createQuery(getTypeClass());
    Root<T> root = query.from(getTypeClass());
    CriteriaQuery<T> select = query.select(root);
    TypedQuery<T> all = manager.createQuery(select);
    return all.getResultList();
}

I did a brief review in the department, added the CDI and its Weld implementation, added the Servlet API and the JPA 2.1 specification.

http://pastebin.com/4AFFChCy

  • Douglas, thanks for the comments. I changed the initial method to public, but it still gives the same error. @Postconstruct public void init(){ establishments = (List<Establishment>) bean.findAll(); establishment = new Establishment(); }

  • in the debug it passes gives error together on line 45 of the Genericdaoimp class. Return entityManager.createQuery("FROM" + getTypeClass().getName()). getResultList();

  • I edited the answer. I understand.

  • Do you know about Reflection, or are you using internet code? It’s nice, for each model, to have your specific DAO.

  • I edited the answer again, try to make use of this Criteria implementation.

  • I made the changes, but without success...

  • if you want to see the full code is in the repository: https://bitbucket.org/umari_tecnologia/erp

  • Same Exception?

  • this time appeared in Portuguese:

  • javax.servlet.Servletexception: An error occurred while performing resource injection on the managed bean establishmentMB

  • it is as if in the Genericdaoimpl class the variables entityManager were coming null...

  • I put in question my debug screen that reaches the class Genericdaoimpl

  • I edited the answer again, try again with these dependencies.

  • I copied the references, but the same error is running.

  • Take advantage and commit and push all modifications made after, for those who help, have an idea.

  • If no one can find the problem, I recommend having a DAO class directly with the implementations without using Genericdao and interfaces that do not represent anything, just force the implementation of CRUD.

Show 11 more comments

Browser other questions tagged

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