jpa mysql table does not exists autoGenerate doesn't work

i am trying to insert a Employee entity into the database. I am using JPA and the database is mysql. When I try to insert the entity, it gives me tables must be created I assume that I dont have to create the table. Entity will automatically create the table with the annotated name. Please find the exception and code below.

    Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gsq.articles' doesn't exist
Error Code: 1146
Call: CREATE TABLE articles (id INTEGER AUTO_INCREMENT NOT NULL, libelle VARCHAR(255), ENTREPRISE_id INTEGER, PRIMARY KEY (id))
Query: DataModifyQuery(sql="CREATE TABLE articles (id INTEGER AUTO_INCREMENT NOT NULL, libelle VARCHAR(255), ENTREPRISE_id INTEGER, PRIMARY KEY (id))")
[EL Fine]: 2017-11-12 20:57:29.914--ServerSession(162116790)--Connection(578752466)--CREATE TABLE entreprise (id INTEGER AUTO_INCREMENT NOT NULL, libelle VARCHAR(255) UNIQUE, PRIMARY KEY (id))
[EL Fine]: 2017-11-12 20:57:29.919--ServerSession(162116790)--SELECT 1
[EL Warning]: 2017-11-12 20:57:29.921--ServerSession(162116790)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gsq.entreprise' doesn't exist
Error Code: 1146
Call: CREATE TABLE entreprise (id INTEGER AUTO_INCREMENT NOT NULL, libelle VARCHAR(255) UNIQUE, PRIMARY KEY (id))
Query: DataModifyQuery(sql="CREATE TABLE entreprise (id INTEGER AUTO_INCREMENT NOT NULL, libelle VARCHAR(255) UNIQUE, PRIMARY KEY (id))")
[EL Fine]: 2017-11-12 20:57:29.922--ServerSession(162116790)--Connection(578752466)--CREATE TABLE users (ID BIGINT AUTO_INCREMENT NOT NULL, password VARCHAR(255), type VARCHAR(255), username VARCHAR(255) UNIQUE, PRIMARY KEY (ID))
[EL Fine]: 2017-11-12 20:57:29.927--ServerSession(162116790)--SELECT 1
[EL Warning]: 2017-11-12 20:57:29.929--ServerSession(162116790)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gsq.users' doesn't exist
Error Code: 1146
Call: CREATE TABLE users (ID BIGINT AUTO_INCREMENT NOT NULL, password VARCHAR(255), type VARCHAR(255), username VARCHAR(255) UNIQUE, PRIMARY KEY (ID))
Query: DataModifyQuery(sql="CREATE TABLE users (ID BIGINT AUTO_INCREMENT NOT NULL, password VARCHAR(255), type VARCHAR(255), username VARCHAR(255) UNIQUE, PRIMARY KEY (ID))")
[EL Fine]: 2017-11-12 20:57:29.93--ServerSession(162116790)--Connection(578752466)--ALTER TABLE articles ADD CONSTRAINT FK_articles_ENTREPRISE_id FOREIGN KEY (ENTREPRISE_id) REFERENCES entreprise (id)
[EL Fine]: 2017-11-12 20:57:29.935--ServerSession(162116790)--SELECT 1
[EL Warning]: 2017-11-12 20:57:29.936--ServerSession(162116790)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gsq.articles' doesn't exist
Error Code: 1146
Call: ALTER TABLE articles ADD CONSTRAINT FK_articles_ENTREPRISE_id FOREIGN KEY (ENTREPRISE_id) REFERENCES entreprise (id)
Query: DataModifyQuery(sql="ALTER TABLE articles ADD CONSTRAINT FK_articles_ENTREPRISE_id FOREIGN KEY (ENTREPRISE_id) REFERENCES entreprise (id)")
[EL Fine]: 2017-11-12 20:57:30.035--ServerSession(162116790)--Connection(578752466)--SELECT id, libelle FROM entreprise
[EL Fine]: 2017-11-12 20:57:30.035--ServerSession(162116790)--SELECT 1
[EL Warning]: 2017-11-12 20:57:30.035--UnitOfWork(1734860123)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'gsq.entreprise' doesn't exist
Error Code: 1146
Call: SELECT id, libelle FROM entreprise
Query: ReadAllQuery(referenceClass=Entreprise sql="SELECT id, libelle FROM entreprise")
Exception in thread "AWT-EventQueue-0" javax.persistence.RollbackException: Transaction rolled back because transaction was set to RollbackOnly.
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:92)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
    at controlleur.Executerequest.closeEntityManager(Executerequest.java:73)
    at frame.ArticlesFram.MajComBox(ArticlesFram.java:385)
    at frame.ArticlesFram.<init>(ArticlesFram.java:53)
    at frame.ArticlesFram$13.run(ArticlesFram.java:501)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    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.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    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)

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
  <persistence-unit name="GSQPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>entities.Articles</class>
    <class>entities.Entreprise</class>
    <class>entities.Users</class>
    <properties>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/gsq"/>
      <property name="javax.persistence.jdbc.password" value=""/>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
      <property name="javax.persistence.jdbc.user" value="root"/>
      <property name="eclipselink.logging.level.sql" value="FINE"/>
      <property name="eclipselink.logging.parameters" value="true"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>

Artcile entity

@Entity
@Table(name = "articles")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Articles.findAll", query = "SELECT a FROM Articles a"),
    @NamedQuery(name = "Articles.findById", query = "SELECT a FROM Articles a WHERE a.id = :id"),
     @NamedQuery(name = "Articles.findByLibelle", query = "SELECT a FROM Articles a WHERE a.libelle = :libelle")})
public class Articles implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;


    @Column(name = "libelle")
    private String libelle;
    @ManyToOne
    private Entreprise entreprise;



    public Articles() {
    }

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


}

1 answer

  • answered 2017-11-12 21:25 nabeel

    I haven't work with eclipselink, but checking a few example and can see that all have mentioned below 4 properties each time:

    <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="eclipselink.create-ddl-jdbc-file-name" value="createDDL_ddlGeneration.jdbc"/>
    <property name="eclipselink.drop-ddl-jdbc-file-name" value="dropDDL_ddlGeneration.jdbc"/>
    <property name="eclipselink.ddl-generation.output-mode" value="both"/>
    

    might be the cause ?