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.