在hibernate 4 - spring 4设置中,可以使用SchemaExport
对象生成DDL:
LocalSessionFactoryBean sfb = (LocalSessionFactoryBean) context.getBean("&sessionFactory");
SchemaExport schema = new SchemaExport(sfb.getConfiguration());
但是hibernate 5用SchemaExport(Configuration configuration)
替换SchemaExport(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);
但是有一个更好的方法会很好,而且,Validator注释(@NotNull,@ Size)不用于DDL生成,我不知道它是否是Hibernate 5或此设置中的错误。< / p>
我正在使用hibernate 5.0.0.CR4 和spring 4.2.0.RELEASE
答案 0 :(得分:2)
您需要实施org.hibernate.integrator.spi.Integrator
,您可以将所需数据存储到某个持有者。
您可以在此处找到工作示例https://github.com/valery-barysok/spring4-hibernate5-stackoverflow-34612019
将其注册为META-INF/services/org.hibernate.integrator.spi.Integrator
文件
public class Integrator implements org.hibernate.integrator.spi.Integrator {
@Override
public void integrate(Metadata metadata, SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
HibernateInfoHolder.setMetadata(metadata);
HibernateInfoHolder.setSessionFactory(sessionFactory);
HibernateInfoHolder.setServiceRegistry(serviceRegistry);
}
@Override
public void disintegrate(SessionFactoryImplementor sessionFactory, SessionFactoryServiceRegistry serviceRegistry) {
}
}
使用它
new SchemaExport((MetadataImplementor) HibernateInfoHolder.getMetadata()).create(true, true);
new SchemaUpdate(HibernateInfoHolder.getServiceRegistry(), (MetadataImplementor) HibernateInfoHolder.getMetadata()).execute(true, true);
您可以在此处找到其他信息Programmatic SchemaExport / SchemaUpdate with Hibernate 5 and Spring 4
Java Persistence API有Configuration over Convention
原则,但Validation API仅用于验证目的。验证不是绝对的,您可以在同一个字段上放置不同的验证规则。
如果你有例如
@Size(max = 50)
@NotNull(groups = DefaultGroup.class)
@Null(groups = SecondGroup.class)
private String shortTitle;
然后它被解释为
@Size(max = 50)
@NotNull(groups = DefaultGroup.class)
@Null(groups = SecondGroup.class)
@Column(length = 255, nullable = true)
private String shortTitle;
在此处查看更多详情 Why does Hibernate Tools hbm2ddl generation not take into account Bean Validation annotations?
答案 1 :(得分:2)
对于Hibernate 5.2.7(在我的例子中)我已经写了一个方法来导出基于包扫描的模式,如:
static void exportSchema(
DataSource dataSource,
Class<? extends Dialect> dialect,
String... packagesToScan) {
StandardServiceRegistryBuilder registryBuilder = new StandardServiceRegistryBuilder()
.applySetting(DATASOURCE, dataSource)
.applySetting(DIALECT, dialect); // dialect could be omitted
MetadataSources metadataSources = new MetadataSources(registryBuilder.build());
PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
new LocalSessionFactoryBuilder(null, resourceLoader, metadataSources)
.scanPackages(packagesToScan);
Metadata metadata = metadataSources.buildMetadata();
new SchemaExport()
.setFormat(true)
.create(EnumSet.of(STDOUT, DATABASE), metadata);
}