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 ?

4
  • 1
    Those are ORM libraries like Hibernate, TopLink and DataNucleus. Commented Aug 8, 2014 at 16:59
  • what he said. Java is statically typed. Commented Aug 8, 2014 at 17:04
  • Duplicate as well of stackoverflow.com/questions/4794777/… Commented Aug 8, 2014 at 17:04
  • I have never used Hibernate, but from what I am guessing they are based on `xml' files. At least, That's how they do it in my company's ORM. But I don't want to read fem a xml file, but from a sql file. Commented Aug 8, 2014 at 17:05

1 Answer 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.