在 Hibernate 4 - Spring 4 的设置中,可以使用 SchemaExport
对象来生成 DDL:
LocalSessionFactoryBean sfb = (LocalSessionFactoryBean) context.getBean("&sessionFactory");
SchemaExport schema = new SchemaExport(sfb.getConfiguration());
但是Hibernate 5用SchemaExport(MetadataImplementator metadataImplementator)
代替了SchemaExport(Configuration configuration)
构造函数。
然而,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);
但是有一个更好的方法会很好,同时,验证器注释(@NotNull,@Size)没有用于DDL生成,我不知道这是Hibernate 5中的错误还是设置错误。
我正在使用hibernate 5.0.0.CR4和spring 4.2.0.RELEASE
org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
。你有什么建议为什么会发生这种情况吗? - LucadataSource
... - Timofey Gorshkov