我如何从Java代码为我的jpa实体创建一个ddl? [英] How can I create a ddl for my jpa entities from java code?

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

问题描述

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



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


从数据库导出数据为sql 使用 href =http://www.liquibase.org/ =nofollow noreferrer> liquibase opensource project


LiquiBase是一个开放源码(LGPL),与数据库无关的库,用于跟踪,管理和应用数据库更改。它建立在一个简单的前提上:所有数据库更改(结构和数据)都以基于XML的描述性方式进行存储并签入源代码管理。

为给定的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

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

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆