在hibernate 4 - spring 4 setup中,可以使用SchemaExportobject 生成DDL :
LocalSessionFactoryBean sfb = (LocalSessionFactoryBean) context.getBean("&sessionFactory");
SchemaExport schema = new SchemaExport(sfb.getConfiguration());
Run Code Online (Sandbox Code Playgroud)
但是hibernate 5替换了SchemaExport(Configuration configuration)构造函数SchemaExport(MetadataImplementator metadataImplementator).
MetadataImplementator并非随时可用
org.springframework.orm.hibernate5.LocalSessionFactoryBean 要么 org.springframework.orm.hibernate5.LocalSessionFactoryBuilder
我这样砍了它:
MetadataSources metadataSources = (MetadataSources) FieldUtils.readField(configuration, "metadataSources", true);
Metadata metadata = metadataSources
.getMetadataBuilder(configuration.getStandardServiceRegistryBuilder().build())
.applyPhysicalNamingStrategy(new MyPhysicialNamingStrategy())
.applyImplicitNamingStrategy(ImplicitNamingStrategyJpaCompliantImpl.INSTANCE)
.build();
MetadataImplementor metadataImpl = (MetadataImplementor) metadata;
SchemaExport schema = new SchemaExport(metadataImplementor);
Run Code Online (Sandbox Code Playgroud)
但是有一个更好的方法会很好,而且Validator注释(@NotNull,@ Size)不用于DDL生成,我不知道它是否是Hibernate 5或此设置中的错误.
我正在使用hibernate 5.0.0.CR4和spring 4.2.0.RELEASE