Schemas generation through JPA 2.1 EclipseLink

679 views Asked by At

I am trying to understand all the properties for schemas generation of JPA 2.1 using EclipseLink implementation and a Mysql database, but I had several bugs easy to explain but difficult to debug

Here is my persistence.xml document

    <?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="fairHandlerPU" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>nrz.fairHandler.jpa.Unit</class>
        <class>nrz.fairHandler.jpa.Weight</class>
        <class>nrz.fairHandler.jpa.Pruduct</class>
        <shared-cache-mode>NONE</shared-cache-mode>
        <properties>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/fairhandlerdb?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=""/>         
            <property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>
            <property name="javax.persistence.schema-generation.database.action" value="create"/>         
        </properties>
    </persistence-unit>
</persistence>

Everything work fine in execution if the schema 'fairhandlerdb' is already created, whether the 'fairhandlerdb' tables exists or not, however, if the schema don't exist it will return an exception at runtime:

Exception in thread "main" javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'fairhandlerdb'

which for me is a contradiction since this property job

 <property name="javax.persistence.schema-generation.create-database-schemas" value="true"/>

is to create schema if this one does not exist.

After that, I tried to resolve the problem in another way by executing a creating schemas script, by adding these properties:

<property name="javax.persistence.schema-generation.script.create-source" value="script-then-metadata"/>
<property name="javax.persistence.schema-generation.script.drop-source" value="metadata"/>     
<property name="javax.persistence.schema-generation.script.create-script-source" value="META-INF/createSchema.sql"/>

where createSchema.sql contain only one query:

CREATE SCHEMA `fairhandlerdb` ;

But it does not work neither when the database schema does not already exist at execution time.

My question is, why the two methods used above, didn't work for generating the database schema?

Thank you for your help

0

There are 0 answers