Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

When you already have a database and you want to use JPA or Hibernate to access that database you need something that will generate those POJO classes for you with the appropriate annotations. There are some existing solutions but I was not happy with them, so this is Yet Another Pojo Generator.

The generator gets generates as part of the build of DomUI and can be found as utilities/hibernate-generator/target/hibernate-generator.jar, and can be executed as:

java -jar hibernate-generator.jar

The generator so far should work for PostgreSQL and Oracle, with PostgreSQL the one that has been tested. Adding new database types should not be too hard.

The generator does the following:

  • It generates a new POJO class for every table in the schema(s) specified
  • If a POJO class already exists for the table it edits the POJO file and updates it with the database definition:
    • New columns are added to the existing POJO
    • Removed columns are, well, removed
    • Metadata is updated where applicable
  • Existing code in POJO classes is mostly left alone, so it should be reasonably safe to re-run the generator on already existing code.
  • It has basic support for compound primary keys; it should properly generate XxxxId classes for class Xxxx if that table as a compound primary key
  • It adds all relations that are properly defined by foreign keys
    • It adds @ManyToOne parent references in the child class
    • It adds @OneToMany child list references as List<T> in the parent, with T the type of the child class
    • This however will not work for compound PK's.
  • It generates/updates [classname].properties files with the new properties in the class, so that these files can be used as resource bundles for the property names of that class.
  • It generates a class HibernateConfigurator which contains methods that add all classes to Hibernate's config.


An example command line invocation would be:

java -jar hibernate-generator.jar -dbtype postgres -db dbusername:dbpasswordl@localhost/database_name -pkgroot org.mydomain.myprogram.database -source /home/jal/myproject/src/main/java -s pdi_meta -s source_mapping -s auth -s definition -s sectormodel

This connects to a PostgreSQL database with the specified username and passwords, loads all tables from the schema's pdi_meta, source_mapping, auth and sectormodel, then generates/updates all classes into the specified directory and package.

The directory and package are separate, so the final directory for the classes is formed by adding the source AND the package name.

  • No labels