tags:

views:

549

answers:

2

is there any hibernate utitliy that i can use to connect to database. list all the tables, and generate sql script for "creating tables + data" and save as *.sql ?

+2  A: 

Export data from a database as sql

Use the liquibase opensource project

LiquiBase is an open source (LGPL), database-independent library for tracking, managing and applying database changes. It is built on a simple premise: All database changes (structure and data) are stored in an XML-based descriptive manner and checked into source control.

Generate create and drop script for given JPA entities

We use this code to generate the drop and create statements: Just construct this class with all entity classes and call create/dropTableScript.

If needed you can use a persitence.xml and persitance unit name instead. Just say something and I post the code too.

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();
    }
}

KlausMeier
this required you to have the domain object in order to convert to sql script right? what i want is. just connect to database server and list all tables, and generate create tables script. possible?
cometta
Right you need the enities (in perstiance.xml or in a List<Class<?>>.Else you can use liquibase, which exports a ddl schema inclusive data from a given jdbc connection. It can do a lot more thinks (schema diffing, patching ...).An other way is to use the swiss army knife DBVisualizer at work our absoulte favorite (in personal edition free!, written in java uses JDBC).
KlausMeier
hi, i tried to use dbvisualizer, i can see --> table, view" for my database. how to export tables and data into sql script?
cometta
Hi, with free edition you can select a table and then open the ddl tab. the data can be exported in the data tab.The personal edition (http://www.minq.se/products/dbvis/features/features.jsp?page=matrix , ca. 150$ or for edu 120$, dbvis is every penny worth, we use >2Year for linux, mac, windows) can export all together for all tables (as sql, xml, ...). There for you must select the database (below databases) in the context menu to export the whole database!
KlausMeier
Btw. if you like my answer please place the checkmark on it ;)
KlausMeier
You should try liquibase too. It can export the whole database too.
KlausMeier
A: 

If your are using MS Access database, I recommend you use MDBScript, it is very easy to use.