2

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 ?

OneMoreError
  • 7,518
  • 20
  • 73
  • 112

1 Answers1

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.

Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152