Generate mapping files from pre-existing POJO java files

Hello Experts,

I am looking forward for a way to generate mapping files from pre-existing JAVA classes. JAVA files for these classes don’t have any annotations, but they have some kind of hierarchy.
I know that it is possible to generate mapping files from DB tables, but I don’t have any pre-existing DB tables, but would like to create them using mapping file.

Thanks,
Kailas

Hi @Kailas_Phanashikar - probably you are looking for something like that :slight_smile:
org.hibernate.tool.hbm2ddl.SchemaExport;
org.hibernate.tool.schema.TargetType;

private void export() {
SchemaExport export = new SchemaExport();
export.setOutputFile(OUTPUT_FILE);
export.setFormat(true);
export.setDelimiter(";");
EnumSet types = EnumSet.of(TargetType.SCRIPT);
Metadata metadata = createMetadataSources().buildMetadata();
export.execute(types, SchemaExport.Action.CREATE, metadata);
}

private MetadataSources createMetadataSources() {
MetadataSources metadata =
new MetadataSources(new StandardServiceRegistryBuilder()
.applySetting(“hibernate.dialect”, DIALECT)
.build());

for (String entityPackage : entityPackages) {
  final Reflections reflections = new Reflections(entityPackage);
  for (Class<?> cl :
       reflections.getTypesAnnotatedWith(MappedSuperclass.class)) {
    metadata.addAnnotatedClass(cl);
    log.info(String.format("Mapped = %s", cl.getName()));
  }
  for (Class<?> cl : reflections.getTypesAnnotatedWith(Entity.class)) {
    metadata.addAnnotatedClass(cl);
    log.info(String.format("Mapped = %s", cl.getName()));
  }
}
return metadata;

}