javahibernateschemaexport

Hibernate SchemaExport doesn't generate sql script


Could anybody please help me and give an idea why the code below doesn't generate SQL creation script? Hibernate version is 5.2.9.Final. I tried different approaches, used exact class names, package location, connection to an existing database but unfortunately, no one of them works for me. All code snippets I found on the internet looks like my one.

Class<PostgreSQL95Dialect> dialect = PostgreSQL95Dialect.class;

        StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
            .applySetting(Environment.DIALECT, dialect)
            .build();

        MetadataSources metadataSources = new MetadataSources(registry);

        Set<Class<?>> entities = new Reflections("").getTypesAnnotatedWith(Entity.class);
        entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));

        System.out.println(String.format("entities : %d", entities.size()));

        Metadata metadata = metadataSources.buildMetadata();

        new SchemaExport()
            .setFormat(true)
            .setDelimiter(";")
            .setOutputFile(String.format("ddl_%s.sql ", dialect.getSimpleName()))
            .execute(EnumSet.of(TargetType.STDOUT, TargetType.SCRIPT), SchemaExport.Action.BOTH, metadata);

        StandardServiceRegistryBuilder.destroy(registry);

The result of code execution is below

    Apr 27, 2017 7:26:43 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.2.9.Final}
Apr 27, 2017 7:26:43 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
entities : 144
Apr 27, 2017 7:26:44 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator initiateService
WARN: HHH000181: No appropriate connection provider encountered, assuming application will be supplying connections
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator initiateService
WARN: HHH000342: Could not obtain connection to query metadata : The application must supply JDBC connections
Apr 27, 2017 7:26:44 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.PostgreSQL95Dialect
Apr 27, 2017 7:26:44 PM org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl makeLobCreatorBuilder
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Apr 27, 2017 7:26:44 PM org.hibernate.type.BasicTypeRegistry register
INFO: HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@5b239d7d
Apr 27, 2017 7:26:44 PM org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export
Apr 27, 2017 7:26:44 PM org.hibernate.tool.schema.internal.SchemaCreatorImpl applyImportSources
INFO: HHH000476: Executing import script 'org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl@74455848'

I would be grateful for any help.


Solution

  • Finally, I have found a root of evil :)

    I created a test Entity and tried to generate script only for it and the code worked! So the only possible place with a mistake is in getting classes with the help of reflection. And shame on me, how inattentive I was. Instead of writing

    entities.forEach(e -> metadataSources.addAnnotatedClass(e.getClass()));
    

    I need this

    entities.forEach(e -> metadataSources.addAnnotatedClass(e));
    

    First one will always give

    class java.lang.Class