JavaEar 专注于收集分享传播有价值的技术资料

从SQL DDL语句创建Java对象 (Creating Java objects from SQL DDL statements)

I am looking for some existing library in java which is capable of parsing a SQL DDL statements, which is capable of doing this:

Say I have a table as follows :

create table emp
(
  name varchar(20),
  empid int,
  date_of_joining date
);

Then, I want a class with the following constructor:

Emp(String name, int empid, Date date_of_joining)

Now, if we someone changes the schema of the table as follows:

alter table emp 
add salary double;

Emp(String name, int empid, Date date_of_joining, double salary)

alter table emp 
modify empid varchar(6);

Emp(String name, String empid, Date date_of_joining, double salary)

Is there something already existing, which does something lie this ?

1个回答

    最佳答案

  1. 英文原文

    Use Hibernate Tools Reverse Engineering.

    It doesn't parse raw SQL files, but can connect to a SQL database, and generate Java POJOs from your schema.

    http://docs.jboss.org/tools/4.1.0.Final/en/hibernatetools/html_single/index.html#refeng_codegen

    It won't make parameterized constructors for you out-of-the-box, but you could customize its templates, or use Lombok:

    http://projectlombok.org/features/Constructor.html

    Frankly, I'd go the other way. Update the database schema from Java POJOs. Why? Database Table Inheritance. It's easy to declare using JPA, but there's no common pattern for declarative table inheritance in SQL.


    中文翻译

    使用Hibernate Tools逆向工程。

    它不解析原始SQL文件,但可以连接到SQL数据库,并从模式生成Java POJO。

    http:// docs。 jboss.org/tools/4.1.0.Final/en/hibernatetools/html_single/index.html#refeng_codegen

    它不会为您提供开箱即用的参数化构造函数,但您可以自定义其模板,或使用Lombok:

    http://projectlombok.org/features/Constructor.html

    坦率地说,我会采用其他的方式。从Java POJO更新数据库模式。为什么?数据库表继承。使用JPA很容易声明,但SQL中的声明性表继承没有通用模式。

    Use Hibernate Tools Reverse Engineering.

    It doesn't parse raw SQL files, but can connect to a SQL database, and generate Java POJOs from your schema.

    http://docs.jboss.org/tools/4.1.0.Final/en/hibernatetools/html_single/index.html#refeng_codegen

    It won't make parameterized constructors for you out-of-the-box, but you could customize its templates, or use Lombok:

    http://projectlombok.org/features/Constructor.html

    Frankly, I'd go the other way. Update the database schema from Java POJOs. Why? Database Table Inheritance. It's easy to declare using JPA, but there's no common pattern for declarative table inheritance in SQL.

    使用Hibernate Tools逆向工程。

    它不解析原始SQL文件,但可以连接到SQL数据库,并从模式生成Java POJO。

    http:// docs。 jboss.org/tools/4.1.0.Final/en/hibernatetools/html_single/index.html#refeng_codegen

    它不会为您提供开箱即用的参数化构造函数,但您可以自定义其模板,或使用Lombok:

    http://projectlombok.org/features/Constructor.html

    坦率地说,我会采用其他的方式。从Java POJO更新数据库模式。为什么?数据库表继承。使用JPA很容易声明,但SQL中的声明性表继承没有通用模式。