开发者

Is there a framework for database reverse engineering? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
开发者_StackOverflow中文版

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 7 years ago.

Improve this question

I would like to generate custom domain types for our web framework. We have the engineering part where we can model domain types which will then generated into Java code with JPA Annotations.

I considered a reversed approach, where you can specify the database schema and get the generated models which are now compatible vice-versa.

Is there a framework where you can intercept the generation, something like the Hibernate POJO generation. It should be possible to access the tables, get the column names, types, relations, primary keys, easily via Java. The rest would be up to me to generate the correct models.

Any suggestions how to start?


Tables, column names, indexes and so on can be obtained using standard jdbc classes, see DatabaseMetaData. Here is some code that might get you started:

static void dumpResultSet(ResultSet rs) throws SQLException {
    ResultSetMetaData md = rs.getMetaData();
    int columnCount = md.getColumnCount();
    PrintStream out = System.out;
    while (rs.next()) {
        out.print("{\n");
        for (int i=1; i<=columnCount; i++) {
            out.print("    ");
            out.print(md.getColumnLabel(i));
            out.print(": ");
            out.print(rs.getObject(i));

            if (i<columnCount-1) {
                out.print(", ");
            }
            out.print("\n");
        }
        out.print("}\n");
    }
}

Connection con = ...;
DatabaseMetaData md = con.getMetaData();

dumpResultSet(md.getTables(null, null, null, new String[]{"TABLE", "VIEW"}));
dumpResultSet(md.getColumns(null, null, "TABLE_NAME", null));
dumpResultSet(md.getExportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getImportedKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getPrimaryKeys(null, null, "TABLE_NAME"));
dumpResultSet(md.getIndexInfo(null, null, "TABLE_NAME", false, true));


For reverse engineering you can use Spring Roo / Jboss forge or Play framework. Spring Roo is best for incremental scaffolding. You can generate code by executing just 2 commands with Roo.

Thanks


I would use something like codesmith. It is language agnostic and can go over a database to create the models you want. http://www.codesmithtools.com/

Before any ORMs came out for android I used it to generate my models for sqlite. It worked very nicely.


I have made a tool just for that: DB Importer. It generates JPA classes from a database schema. The code generation is highly configurable with a Groovy script. The Groovy syntax used in this script is just like Java.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜