error in persistence.xml, problems with hibernate. persistence

0

In the last few days I have been trying to make a web application for JSP, java and mysql connector 8. I have given an error when I try to execute it, I guess it is the order in the persistence but I have nothing in particular

my mistake is

org.hibernate.ejb.HibernatePersistence         federation         org.eclipse.persistence.jpa.PersistenceProvider

my code of the model class is:

package model;

import java.io.Serializable;    import javax.persistence. *;

/ **     * The persistent class for the USER database table.     *     * /    @Entity    @Table (name="USER")    @NamedQuery (name="User.findAll", query="SELECT u FROM User u")    public class User implements Serializable {     private static final long serialVersionUID = 1L;

private int idUSUARIO;

private int UNIVERSIDAD_idUNIVERSIDAD;

public Usuario() {
}

public int getIdUSUARIO() {
    return this.idUSUARIO;
}

public void setIdUSUARIO(int idUSUARIO) {
    this.idUSUARIO = idUSUARIO;
}

public int getUNIVERSIDAD_idUNIVERSIDAD() {
    return this.UNIVERSIDAD_idUNIVERSIDAD;
}

public void setUNIVERSIDAD_idUNIVERSIDAD(int UNIVERSIDAD_idUNIVERSIDAD) {
    this.UNIVERSIDAD_idUNIVERSIDAD = UNIVERSIDAD_idUNIVERSIDAD;
}

} my control class code is:

import model.Usuario;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

@Entity
public class ControlUsuario {

    private EntityManagerFactory emf;
    private EntityManager em;
    private List<Usuario> _Usuario;
    private Usuario Usuario;

    public ControlUsuario() {
        this.emf = Persistence.createEntityManagerFactory("federacion");
        this.em = this.emf.createEntityManager();
        this._Usuario = consultarUsuario();
        this.Usuario = new Usuario();
    }

    public List<Usuario> consultarUsuario() {
        String jpql = " select * from USUARIO";
        Query query = this.em.createQuery(jpql);
        List<Usuario> _Usuario = query.getResultList();
        return _Usuario;
    }

    public Usuario loginUsuario(Usuario usuario) {
        for (int a = 0; a < _Usuario.size(); a++) {
            if (usuario.getIdUSUARIO() == _Usuario.get(a).getIdUSUARIO()
                    && usuario.getUNIVERSIDAD_idUNIVERSIDAD() == _Usuario.get(a).getUNIVERSIDAD_idUNIVERSIDAD()) {
                System.out.println("logeo satisfactorio");
            } else {
                System.out.println("logeo no satisfactorio");
                usuario = null;
            }
        }
        return usuario;
    }

    public void crearUsuario() {
        try {
            this.em.getTransaction().begin();
            this.em.persist(Usuario);
            this.em.getTransaction().commit();
            this.Usuario = new Usuario();
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    public List<Usuario> get_Usuario() {
        return _Usuario;
    }

    public void set_Usuario(List<Usuario> _Usuario) {
        this._Usuario = _Usuario;
    }

    public Usuario getUsuario() {
        return Usuario;
    }

    public void setUsuario(Usuario Usuario) {
        this.Usuario = Usuario;
    }
}

My jsp code where the conflict is created is when you start the UserControl class:

<%
    ControlUsuario controlusuario = new ControlUsuario();
    Usuario usuario = new Usuario();
%>

The persistence is:

<persistence version="2.1"
    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">
    <persistence-unit name="federacion">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source>federacion</jta-data-source>
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>model.Servicio</class>
        <class>model.SERVICIO_has_UNIVERSIDAD</class>
        <class>model.Universidad</class>
        <class>model.Usuario</class>
        <class>control.ControlUsuario</class>
        <class>control.ControlServicio</class>
        <class>control.ControlServicio_has_Universidad</class>
        <class>control.ControlUniversidad</class>

        <properties>
            <property name="javax.persistence.jdbc.url"
                value="jdbc:mysql://localhost:3306/federacion" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.driver"
                value="com.mysql.cj.jdbc.Driver" />
            <property name="javax.persistence.jdbc.password"
                value="Unired2018*" />
            <property
                name="javax.persistence.schema-generation.database.action"
                value="create" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.MySQL5InnoDBDialect" />
        </properties>
    </persistence-unit>
</persistence>

My pom, in case that the problem, is:

<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>FEDERACION</groupId>
    <artifactId>FEDERACION</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.1</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.0.3.Final</version>
        </dependency>
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.1</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.9</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.3</version>
        </dependency>
    </dependencies>
</project>
    
asked by SERGIO AUGUSTO CARDONA RUEDA 19.09.2018 в 18:20
source

2 answers

0

these days I have been working on the project, it turns out that I decided to eliminate two lines of persistence and it worked, because it keeps generating errors but they are already mine. the lines you delete are

    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>federacion</jta-data-source>
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

and modify another line

<persistence-unit name="federacion" transaction-type="RESOURCE_LOCAL">
    
answered by 21.09.2018 в 23:07
0

I'll clarify what happens.

In a persistence unit <pesistence-unit> the value of the default transaction type depends on the environment in which JPA is executed.

There are two environments: JavaEE and JavaSE . A JavaEE environment means that the application runs in an application container JavaEE (now JakartaEE ), such as Payara > or WildFly . A JavaSE environment means that the application runs without a JavaEE container, such as a desktop application or a container of web applications such as Tomcat .

The value of the default transaction type ( transaction-type ) of a persistence unit for each environment is:

JavaSE - > RESOURCE_LOCAL

JavaEE - > JTA

Pass the following:

  • <jta-data-source> is added when the environment is JavaEE.
  • <provider> should only be added once. You added it twice.

Something additional:

JPA is an interface, Hibernate or EclipseLink are implementations (in JPA also known as providers or providers). If you are going to use Hibernate you must specify the label of <provider> with the values of the Hibernate provider, in the same way, if you use EclipseLink, the values of the EclipseLink provider.

Explanation to the solution you discovered:

  • As it seems, you're using JPA in a JavaSE environment, so you did not have to put: <jta-data-source>federacion</jta-data-source>
  • Only a <provider> should be specified. However, it is optional. When it is not placed, I think JPA opts it from the added implementation.
  • answered by 10.10.2018 в 19:47