Home » Java » hibernate 5 generate sql ddl for java pojo object [on hold]

hibernate 5 generate sql ddl for java pojo object [on hold]

Posted by: admin July 12, 2018 Leave a comment

Questions:
  • how did it work ?

    • generate ddl for java object

    • plain java object for ddl

    • table name = java name, column name = java field name

The code

static class Demo {
         private String name;
         private String age;
         private String address;
     }
    public static void main(String[] args) throws Exception {

        MetadataSources metadata = new MetadataSources(
                new StandardServiceRegistryBuilder()
                        .applySetting("hibernate.dialect", org.hibernate.dialect.Oracle12cDialect.class)
                        .build());
                 metadata.addAnnotatedClass(Demo.class);
        String filename = "c:\\ddl_" + "test" + ".sql"; // dialect.getSimpleName().toLowerCase()
        MetadataImplementor metadataImplementor = (MetadataImplementor) metadata.buildMetadata();
        SchemaExport export = new SchemaExport();
        export.setHaltOnError(false)
                .setDelimiter(";")
                .setOutputFile(filename)
                .setFormat(true);

        //can change the output here`enter code here`
        EnumSet<TargetType> enumSet = EnumSet.of(TargetType.STDOUT);
//      EnumSet<TargetType> enumSet = EnumSet.of(TargetType.SCRIPT);
        export.execute(enumSet, SchemaExport.Action.CREATE, metadataImplementor);
    }
Answers: