Problem with JPA using Wildfly 9 and JTA

0

It's the first time I try to use JTA using the Wildfly container and so far I have not been able to run my code right. When I publish the application (start Wildfly) it perfectly matches, generates the table and such. But when I enter the screen to display a simple list, an error occurs.

Below is the error screen image:

Ihavealreadysetthedatasourceinwildfly(somuchthatitgeneratesthetableinthedatabase).

I'musingthedaopatternintheproject.

Mypersistence.xml:

<?xmlversion="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'm 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 EstablishmentMB

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:

Repositorycodeavailable: link I am very grateful to anyone who can give me a light ...

    
asked by anonymous 14.12.2015 / 15:55

1 answer

1

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

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

In your MB Enterprise, with @Named of CDI and @RequestScoped of JSF

To use only the CDI, use

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

JSF

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

I'll leave some comments for this using JPA

In dependencies you are using Hibernate 5.0, in this case I also recommend using #, 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>

At the same time, start using <provider> HibernatePersistenceProvider of package org.hibernate.jpa .

The hibernate persistence of the org.hibernate.ejb package has been deprecated in these latest versions of Hibernate.

  

If you still have a problem with the findAll method, try using 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 made a brief revision in dependencies, added the CDI and its Weld implementation, I also added the Servlet API and the JPA 2.1 specification.

link

    
14.12.2015 / 16:12