Problem saving with JPA

1

I am trying to prove a method that I have created to save in a database using JPA, it is as follows:

Through the following class:

public class Prueba {
  UsuarioImpls usuarioIm= new UsuarioImpls();
  public void guardaUsuario(){
    String nombre= "David";
    String apellido= "djsfla";
    String email= "[email protected]";
    String pass= "sdkl";
    int tar= 152;

    usuarioIm.guardar(new Usuario(nombre, apellido, email, pass, tar));
  }

  public static void main(String[] args){
    Prueba p= new Prueba();
    p.guardaUsuario();
  }
}

The previous saveUser method uses another method that is implemented in this other class:

public class UsuarioImpls implements UsuarioDAO, Serializable{

EntityManagerFactory emf= Persistence.createEntityManagerFactory("Tienda_V4.2PU");
EntityManager em= emf.createEntityManager();

@Override
public void guardar(Usuario usuario) {
    em.getTransaction().begin();
    em.persist(usuario);
    em.getTransaction().commit();
}
}

And this is the interface that contains it:

public interface UsuarioDAO<Usuario>{
void guardar(Usuario usuario);
}

The entity class to which the interface refers is this:

@Entity
@Table(name = "usuario")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u")
, @NamedQuery(name = "Usuario.findById", query = "SELECT u FROM Usuario u WHERE u.id = :id")
, @NamedQuery(name = "Usuario.findByFecha", query = "SELECT u FROM Usuario u WHERE u.fecha = :fecha")
, @NamedQuery(name = "Usuario.findByNombre", query = "SELECT u FROM Usuario u WHERE u.nombre = :nombre")
, @NamedQuery(name = "Usuario.findByApellido", query = "SELECT u FROM Usuario u WHERE u.apellido = :apellido")
, @NamedQuery(name = "Usuario.findByEmail", query = "SELECT u FROM Usuario u WHERE u.email = :email")
, @NamedQuery(name = "Usuario.findByPassword", query = "SELECT u FROM Usuario u WHERE u.password = :password")
, @NamedQuery(name = "Usuario.findByTarjeta", query = "SELECT u FROM Usuario u WHERE u.tarjeta = :tarjeta")})
public class Usuario implements Serializable {

private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "ID")
private Integer id;
@Basic(optional = false)
@Column(name = "FECHA")
@Temporal(TemporalType.TIMESTAMP)
private Date fecha;
@Basic(optional = false)
@Column(name = "NOMBRE")
private String nombre;
@Column(name = "APELLIDO")
private String apellido;
@Basic(optional = false)
@Column(name = "EMAIL")
private String email;
@Basic(optional = false)
@Column(name = "PASSWORD")
private String password;
@Column(name = "TARJETA")
private Integer tarjeta;

public Usuario() {
}

public Usuario(Integer id) {
    this.id = id;
}

public Usuario(Integer id, Date fecha, String nombre, String email, String password) {
    this.id = id;
    this.fecha = fecha;
    this.nombre = nombre;
    this.email = email;
    this.password = password;
}

public Usuario(String nombre, String apellido, String email, String password, Integer tarjeta) {
    this.nombre = nombre;
    this.apellido = apellido;
    this.email = email;
    this.password = password;
    this.tarjeta = tarjeta;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public Date getFecha() {
    return fecha;
}

public void setFecha(Date fecha) {
    this.fecha = fecha;
}

public String getNombre() {
    return nombre;
}

public void setNombre(String nombre) {
    this.nombre = nombre;
}

public String getApellido() {
    return apellido;
}

public void setApellido(String apellido) {
    this.apellido = apellido;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public Integer getTarjeta() {
    return tarjeta;
}

public void setTarjeta(Integer tarjeta) {
    this.tarjeta = tarjeta;
}

@Override
public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
}

@Override
public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Usuario)) {
        return false;
    }
    Usuario other = (Usuario) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
        return false;
    }
    return true;
}

@Override
public String toString() {
    return "com.tienda.modelo.entities.Usuario[ id=" + id + " ]";
}

}

Trying to save the StackTrace in the database shows me the following error on:

  

WARN: HHH015016: Encountered to deprecated javax.persistence.spi.PersistenceProvider [org.hibernate.ejb.HibernatePersistence]; use [org.hibernate.jpa.HibernatePersistenceProvider] instead.   Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named Store_V4.2PU       at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:61)       at javax.persistence.Persistence.createEntityManagerFactory (Persistence.java:39)       at com.tienda.dao.implement.UsuarioImpls. (UsuarioImpls.java:14)       at com.tienda.dao.implement.Trueba. (Prueba.java:7)       at com.tienda.dao.implement.Prueba.main (Prueba.java:19)

This is the Persistence:

<?xml version="1.0" encoding="UTF-8"?>
<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="Tienda_V4.2PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tienda.modelo.entities.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tiendadb"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.password" value="itla"/>
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>

I have already changed the provider from persistence to:

org.hibernate.jpa.HibernatePersistence

And it does not work, it keeps giving me the same error of the persistence name

I have also checked the libraries and made sure to have them imported as well as that the Persistence Unit is in the META-INF folder:

With all this error, you can guide me about it.

    
asked by David Calderon 06.12.2016 в 17:58
source

1 answer

1

The stacktrace is telling you precisely that:

The org.hibernate.jpa.HibernatePersistence is obsolete please use: org.hibernate.jpa.HibernatePersistenceProvider Try this to see if it works. Although they are Warning but not Error .

EDIT

Indeed, after having recreated the application, the problem arises due to the erroneous syntax of /META-INF/persistence.xml and "bad" location of this file for Netbeans.

The file as it is in the question is:

<?xml version="1.0" encoding="UTF-8"?>
<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="Tienda_V4.2PU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tienda.modelo.entities.Usuario</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tiendadb"/>
  <property name="javax.persistence.jdbc.user" value="root"/>
  <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
  <property name="javax.persistence.jdbc.password" value="itla"/>
  <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>

It may have been a copy-paste poorly done But, the persistence.xml should be:

<?xml version="1.0" encoding="UTF-8"?>
<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="Tienda_V4.2PU" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>    
        <exclude-unlisted-classes>false</exclude-unlisted-classes>
        <properties>           
            <property name="javax.persistence.jdbc.user" value="root"/>
            <property name="javax.persistence.jdbc.password" value="itla"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/tiendadb"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>        
            <property name="javax.persistence.schema-generation.database.action" value="create"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>                        
        </properties>
    </persistence-unit>
</persistence>

This solution is for Netbeans! because @David had problems using this IDE and the problem leaves the initial configuration.

It is assumed that persistence.xml should go in /META-INF/ and that is the way you have it configured. BUT these configuration files, Netbeans puts them in different places, then when using ANT for the build , puts the files where they should be but from the file conf that we see in Netbeans like this:

The persistence.xml must be in /Configuration Files/ and not in /META-INF/ .

Once I made these corrections the program works without problem.

    
answered by 07.12.2016 / 03:04
source