Error saving to a mySQL table with JPA

1

I have this class to save in a table with JPA:

<línea 12>public class AgendaJPADAOImpl implements AgendaJPADAO, Serializable{

EntityManagerFactory emf= Persistence.createEntityManagerFactory("AgendaJPAPU");
EntityManager em= emf.createEntityManager();

@Override
public void guardar(Contacto c) {
    em.getTransaction().begin();
        em.persist(c);
    <línea 21>em.getTransaction().commit();
}
}

Every time I try to save to the database, it shows me this stacktrace :

Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mysql.contacto' doesn't exist
Error Code: 1146
Call: INSERT INTO contacto (APELLIDO, CASA, DIRECCION, FECHA, MOVIL, NOMBRE, PERSONAL, TRABAJO) VALUES (?, ?, ?, ?, ?, ?, ?, ?)
bind => [8 parameters bound]
Query: InsertObjectQuery(comm.agendaJPA.modelo.Contacto@57a86cd2)
at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:157)
at comm.agendaJPA.dao.impl.AgendaJPADAOImpl.guardar(AgendaJPADAOImpl.java:21)
at comm.agendaJPA.dao.impl.AgendaJPADAOImpl.guardar(AgendaJPADAOImpl.java:12)
at comm.agendaJPA.controlador.AgendaJPAControlador.actionPerformed(AgendaJPAControlador.java:85)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException

It tells me that the contact table does not exist, but if I have it created

The Stacktrace also refers to lines 21 and 12 of the class shown above, marked in this by the way, and finally line 86 of the class AgendaJPAControlador , this is where I instancio the method save class AgendaJPADAOImpl .

case getBtnGuardar:
            Contacto c= new Contacto();
            this.agendaJPADAO.guardar(c);
            break;

My class Contact is structured as follows:

package comm.agendaJPA.modelo;

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

@Entity(name = "Contactom")
@Table(name = "contacto")
@NamedQueries({
@NamedQuery(name = "AgendaJPA.getAll", query = "SELECT c FROM Contactom c")
})
public class Contacto implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Temporal(TemporalType.DATE)
private Calendar fecha;
private String nombre;
private String apellido;
private int movil;
private int casa;
private String personal;
private String trabajo;
private String direccion;

public Contacto() {

}

public Contacto(Calendar fecha, String nombre, String apellido, int movil, int casa, String personal, String trabajo, String direccion) {
    this.fecha = fecha;
    this.nombre = nombre;
    this.apellido = apellido;
    this.movil = movil;
    this.casa = casa;
    this.personal = personal;
    this.trabajo = trabajo;
    this.direccion = direccion;
}

public Contacto(int id, String nombre, String apellido, int movil, int casa,
        String personal, String trabajo, String direccion) {
    this.id = id;
    this.nombre = nombre;
    this.apellido = apellido;
    this.movil = movil;
    this.casa = casa;
    this.personal = personal;
    this.trabajo = trabajo;
    this.direccion = direccion;
}

public int getId() {
    return id;
}

public Calendar getFecha() {
    return fecha;
}

public String getNombre() {
    return nombre;
}

public String getApellido() {
    return apellido;
}

public int getMovil() {
    return movil;
}

public int getCasa() {
    return casa;
}

public String getPersonal() {
    return personal;
}

public String getTrabajo() {
    return trabajo;
}

public String getDireccion() {
    return direccion;
}

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

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

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

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

public void setMovil(int movil) {
    this.movil = movil;
}

public void setCasa(int casa) {
    this.casa = casa;
}

public void setPersonal(String personal) {
    this.personal = personal;
}

public void setTrabajo(String trabajo) {
    this.trabajo = trabajo;
}

public void setDireccion(String direccion) {
    this.direccion = direccion;
}

}

This is the code of the PersistenceUnit:

<?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="AgendaJPAPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>comm.agendaJPA.modelo.Contacto</class>
<properties>
  <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull"/>
  <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"/>
</properties>
</persistence-unit>
</persistence>

I do not know why I get that exception.

    
asked by David Calderon 17.10.2016 в 14:33
source

1 answer

2

Segpun I see in the images you provide, the database you want to connect to is called agenda , while in your connection string you indicate that you should go to the database mysql . Change your connection string in the persistence unit:

<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/agenda?zeroDateTimeBehavior=convertToNull"/>
                                                                               ^aquí
    
answered by 17.10.2016 / 16:28
source