Where did Configuration.generateSchemaCreationScript() go in Hibernate 5

后端 未结 5 1776
一向
一向 2020-12-01 07:23

In Hibernate 4.x, I used to generate and export the schema as defined in annotated entities as follows (using Spring to find annotated entities on the class path):



        
5条回答
  •  情话喂你
    2020-12-01 08:16

    The new bootstrap API allows for many customizations, but assuming you don't need those, the shortest invocation would look like that, applying default values for service registries and all the settings:

    Metadata metadata = new MetadataSources()
        .addAnnotatedClass( MyEntity.class )
        .build();
    
    new SchemaExport( (MetadataImplementor) metadata )
        .setOutputFile( "my-statements.ddl" )
        .create( Target.NONE );
    

    Update: Providing example for applying configuration propperties

    There are several ways to inject properties for connection URL, dialect etc. E.g. you could provide a file hibernate.properties or you use a service registry customized with the required settings:

    StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
        .applySetting( "hibernate.connection.url", "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" )
        .build();
    
    Metadata metadata = new MetadataSources( registry )
        .build();
    

提交回复
热议问题