سؤال

I want to automatically insert data into my MySQL tables. Therefore I try to use the JPA property "javax.persistence.sql-load-script-source" in my 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="my-PU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/coding</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
      <property name="javax.persistence.ddl-drop-script-source" value="drop.sql" />
      <property name="javax.persistence.sql-load-script-source" value="insert.sql"/>
    </properties>
  </persistence-unit>
</persistence>

The SQL scripts (drop.sql & insert.sql) are store in "src/main/resources" and include SQL commands. Unfortunately, these commands are not executed. Did I forget a property?

هل كانت مفيدة؟

المحلول

What version of EclipseLink are you using? These properties are part of JPA 2.1, so you must be using the EclipseLink 2.5 version (not yet released).

Enable logging on finest to see if any errors are occurring.

نصائح أخرى

A more general answer adding to James is that JPA 2.1 is part of the JEE7 specification. A JEE6 (which specifies JPA 2.0) compatible application server cannot use these properties (without patching the server for JPA 2.1):

<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
<property name="javax.persistence.schema-generation.create-source" value="script"/>
<property name="javax.persistence.schema-generation.create-script-source" value="META-INF/sql/create.sql" />
<property name="javax.persistence.sql-load-script-source" value="META-INF/sql/data.sql" />
<property name="javax.persistence.schema-generation.drop-source" value="script" />
<property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/sql/drop.sql" />

An example of an application server implementing JEE6 is Jboss 7. Wildfly 8, however, implements JEE7 and these properties should be available for use.

A hint if you are not really sure is to observe the version attribute on the persistence tag. In this case, it is 2.0. If it is specified as 2.1, and properly configured so, JPA 2.1 is used and the properties will work.

I think the problem is in the value of the property. You must set the location in relation to the src. Something like this:

<properties>
  <property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
  <property name="javax.persistence.ddl-drop-script-source" value="main/resources/drop.sql" />
  <property name="javax.persistence.sql-load-script-source" value="main/resources/insert.sql"/>
</properties>

I recommend you visit this link for more information about Database Schema Creation

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top