2009-06-30 7 views

Répondre

2

Exporter des données à partir d'une base de données SQL

Utilisez le projet liquibase opensource

LiquiBase est une open source (LGPL), une bibliothèque de base de données indépendante pour le suivi, la gestion et l'application des modifications de base de données. Il repose sur une prémisse simple: toutes les modifications de la base de données (structure et données) sont stockées de manière descriptive XML et vérifiées dans le contrôle source.

créer Generate et un script drop pour les entités données JPA

Nous utilisons ce code pour générer la chute et de créer des déclarations: Il suffit de construire cette classe avec toutes les classes d'entités et appellent créer/dropTableScript.

Si nécessaire, vous pouvez utiliser un nom de persitence.xml et de persitance. Il suffit de dire quelque chose et je poste le code aussi.

 
import java.util.Collection; 
import java.util.Properties; 

import org.hibernate.cfg.AnnotationConfiguration; 
import org.hibernate.dialect.Dialect; 
import org.hibernate.ejb.Ejb3Configuration; 

/** 
* SQL Creator for Tables according to JPA/Hibernate annotations. 
* 
* Use: 
* 
* {@link #createTablesScript()} To create the table creationg script 
* 
* {@link #dropTablesScript()} to create the table destruction script 
* 
*/ 
public class SqlTableCreator { 

    private final AnnotationConfiguration hibernateConfiguration; 
    private final Properties dialectProps; 

    public SqlTableCreator(final Collection> entities) { 

     final Ejb3Configuration ejb3Configuration = new Ejb3Configuration(); 
     for (final Class entity : entities) { 
      ejb3Configuration.addAnnotatedClass(entity); 
     } 

     dialectProps = new Properties(); 
     dialectProps.put("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect"); 

     hibernateConfiguration = ejb3Configuration.getHibernateConfiguration(); 
    } 

    /** 
    * Create the SQL script to create all tables. 
    * 
    * @return A {@link String} representing the SQL script. 
    */ 
    public String createTablesScript() { 
     final StringBuilder script = new StringBuilder(); 

     final String[] creationScript = hibernateConfiguration.generateSchemaCreationScript(Dialect 
       .getDialect(dialectProps)); 
     for (final String string : creationScript) { 
      script.append(string).append(";\n"); 
     } 
     script.append("\ngo\n\n"); 

     return script.toString(); 
    } 

    /** 
    * Create the SQL script to drop all tables. 
    * 
    * @return A {@link String} representing the SQL script. 
    */ 
    public String dropTablesScript() { 
     final StringBuilder script = new StringBuilder(); 

     final String[] creationScript = hibernateConfiguration.generateDropSchemaScript(Dialect 
       .getDialect(dialectProps)); 
     for (final String string : creationScript) { 
      script.append(string).append(";\n"); 
     } 
     script.append("\ngo\n\n"); 

     return script.toString(); 
    } 
} 

+0

Merci pour la réponse, est-il possible d'utiliser un persistence.xml? –

2

Hibernate a un support intégré pour cela. Voir org.hibernate.tool.hbm2ddl.SchemaExport.

2

OpenJPA a également un support pour cela. L'OpenJPA mapping tool peut créer un script ou créer un fichier ddl. Le ddl devrait fonctionner avec d'autres implémentations JPA (bien que chaque vendeur a quelques bizarreries).

Si vous utilisez OpenJPA en tant que fournisseur de persistance, vous pouvez configurer OpenJPA pour créer les tables la première fois qu'elles sont nécessaires en ajoutant la propriété SynchronizeMappings à persistence.xml.

Exemple:

<persistence-unit name="test"> 
    <!-- 
     . . . 
     --> 
    <properties> 
     <property name="openjpa.jdbc.SynchronizeMappings" 
        value="buildSchema"/> 
    </properties> 
    <!-- 
     . . . 
     --> 
</persistence-unit> 
3

DataNucleus a SchemaTool qui peut être appelé à partir de Java, ou à partir de la ligne de commande. Il fait ce que vous avez besoin

--Andy (DataNucleus)