2014-12-05 9 views
8

Sembra che lo hibernate3-maven-plugin utilizzato per generare gli script di creazione/rilascio DDL non sia più compatibile con Hibernate 4.3 e versioni più recenti (utilizzando JPA 2.1).Genera script DDL su build MAVEN con Hibernate4/JPA 2.1

Io uso questa configurazione del plugin:

  <plugin> 
       <groupId>org.codehaus.mojo</groupId> 
       <artifactId>hibernate3-maven-plugin</artifactId> 
       <version>3.0</version> 
       <executions> 
        <execution> 
         <id>generate-sql-schema</id> 
         <phase>process-sources</phase> 
         <goals> 
          <goal>hbm2ddl</goal> 
         </goals> 
         <configuration> 
          <hibernatetool> 
           <jpaconfiguration persistenceunit="${persistenceUnitName}" /> 
           <hbm2ddl update="true" create="true" export="false" 
            outputfilename="src/main/sql/schema.sql" format="true" 
            console="true" /> 
          </hibernatetool> 
         </configuration> 
        </execution> 
       </executions> 
      </plugin> 

ma ottengo il seguente errore:

[ERROR] Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:3.0:hbm2ddl (generate-sql-schema) on project my-project: There was an error creating the AntRun task. 
An Ant BuildException has occured: java.lang.NoClassDefFoundError: org/hibernate/util/ReflectHelper: org.hibernate.util.ReflectHelper -> [Help 1] 

questa classe come la migrazione a un nuovo pacchetto: org.hibernate.internal.util.ReflectHelper

Tuttavia ho trovato alcuna chiara modo per continuare a generare DDL creare script su build MAVEN.

Non c'è il hibernate4-maven-plugin o nessun altro modo ufficiale per farlo.

Quindi cosa? Non è una caratteristica principale che dovrebbe essere supportata? Come farlo ?

+2

Cosa ne pensi di [hibernate-maven-plugin 4.3.1 Final] (http://mvnrepository.com/artifact/org.hibernate/hibernate-maven-plugin/4.3.1.Final)? – wypieprz

risposta

20

Come Hibernate 4.3+ ora implementa JPA 2.1 il modo appropriato per generare script DDL è quello di utilizzare seguente serie di JPA 2.1 proprietà:

<property name="javax.persistence.schema-generation.scripts.action" value="create"/> 
<property name="javax.persistence.schema-generation.create-source" value="metadata"/> 
<property name="javax.persistence.schema-generation.scripts.create-target" value="target/jpa/sql/create-schema.sql"/> 

un bel riassunto di Altri beni e del contesto di generazione dello schema in JPA 2.1 può essere trovato qui: https://blogs.oracle.com/arungupta/entry/jpa_2_1_schema_generation

E ufficiali JPA 2.1 specifiche qui: https://jcp.org/aboutJava/communityprocess/final/jsr338/index.html

Poiché verrà generato in fase di runtime, è possibile che si desideri eseguire questa generazione DDL alla build.

Qui è l'approccio JPA 2.1 per generare questo script di programmazione:

import java.io.IOException; 
import java.util.Properties; 

import javax.persistence.Persistence; 

import org.hibernate.jpa.AvailableSettings; 

public class JpaSchemaExport { 

    public static void main(String[] args) throws IOException { 
     execute(args[0], args[1]); 
     System.exit(0); 
    } 

    public static void execute(String persistenceUnitName, String destination) { 
     System.out.println("Generating DDL create script to : " + destination); 

     final Properties persistenceProperties = new Properties(); 

     // XXX force persistence properties : remove database target 
     persistenceProperties.setProperty(org.hibernate.cfg.AvailableSettings.HBM2DDL_AUTO, ""); 
     persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_DATABASE_ACTION, "none"); 

     // XXX force persistence properties : define create script target from metadata to destination 
     // persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SCHEMAS, "true"); 
     persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_ACTION, "create"); 
     persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_CREATE_SOURCE, "metadata"); 
     persistenceProperties.setProperty(AvailableSettings.SCHEMA_GEN_SCRIPTS_CREATE_TARGET, destination); 

     Persistence.generateSchema(persistenceUnitName, persistenceProperties); 
    } 

} 

Come si può vedere è molto semplice!

Ora è possibile utilizzare questo in un AntTask, o MAVEN costruire come questo (per MAVEN):

<plugin> 
    <artifactId>maven-antrun-plugin</artifactId> 
    <version>1.7</version> 
    <executions> 
     <execution> 
      <id>generate-ddl-create</id> 
      <phase>process-classes</phase> 
      <goals> 
       <goal>run</goal> 
      </goals> 
      <configuration> 
       <target> 
        <!-- ANT Task definition --> 
        <java classname="com.orange.tools.jpa.JpaSchemaExport" 
         fork="true" failonerror="true"> 
         <arg value="${persistenceUnitName}" /> 
         <arg value="target/jpa/sql/schema-create.sql" /> 
         <!-- reference to the passed-in classpath reference --> 
         <classpath refid="maven.compile.classpath" /> 
        </java> 
       </target> 
      </configuration> 

     </execution> 
    </executions> 
</plugin> 

Si noti che il funzionario hibernate-maven-plugin anche può, o non può, fare il trucco in qualche modo:

<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-maven-plugin</artifactId> 
    <version>4.3.1.Final</version> 
</dependency> 

Divertiti! :)

+0

di gran lunga la soluzione migliore che ho trovato su questo ... – ruckc

+0

grazie @ruckc ma hai provato il http: // mvnrepository.com/artifact/org.hibernate/hibernate-maven-plugin/4.3.1.Final come detto wypieprz? – Donatello

+0

Ciao. Ho provato la soluzione con file gradle e no persistence.xml e sto riscontrando problemi. Ho aperto un post separato: http://stackoverflow.com/questions/30225183. Qualcuno può aiutare, per favore? – balteo