本文介绍了我如何从Java代码为我的jpa实体创建一个ddl?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻找一种方法,可以为我的jpa注释实体创建一个ddl。
我更喜欢纯java方式。



如果可能的话,生成drop语句也会很好。


从数据库导出数据为sql 使用 opensource project

为给定的JPA实体生成创建和删除脚本



我们使用这段代码来生成drop和create语句:
只需用所有的实体类构造这个类并调用create / dropTableScript即可。



需要您可以使用persitence.xml和persistence单元名称。只要说一些
并发布代码即可。

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

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

/ **
*根据JPA / Hibernate注释的SQL Creator for Tables。
*
*使用:
*
* {@link #createTablesScript()}创建表creationg脚本
*
* {@link# dropTablesScript()}创建表破坏脚本
*
* /
public class SqlTableCreator {

private final AnnotationConfiguration hibernateConfiguration;
private final属性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();
}

/ **
*创建SQL脚本来创建所有表。
*
* @return代表SQL脚本的{@link String}。
* /
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(; \\\
);
}
script.append(\\\
go\\\
\\\
);

return script.toString();
}

/ **
*创建SQL脚本以删除所有表。
*
* @return代表SQL脚本的{@link String}。
* /
public String dropTablesScript(){
final StringBuilder script = new StringBuilder();

final String [] creationScript = hibernateConfiguration.generateDropSchemaScript(方言
.getDialect(dialectProps));
for(final String string:creationScript){
script.append(string).append(; \\\
);
}
script.append(\\\
go\\\
\\\
);

return script.toString();
}
}


I look for a way how I can create a ddl for my jpa annotated entities.I prefer a pure java way for this.

If possible it would be nice to have generate the drop statements too.

解决方案

Export data from a database as sql

Use the liquibase opensource project

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

这篇关于我如何从Java代码为我的jpa实体创建一个ddl?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:06