10

在 hibernate 4 - spring 4 设置中,可以使用SchemaExport对象生成 DDL:

LocalSessionFactoryBean sfb = (LocalSessionFactoryBean) context.getBean("&sessionFactory");
SchemaExport schema = new SchemaExport(sfb.getConfiguration());  

但是 hibernate 5SchemaExport(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);

但是最好有更好的方法,而且验证器注释(@NotNull,@Size)不用于 DDL 生成,我不知道这是 Hibernate 5 还是这个设置中的错误。

我正在使用休眠5.0.0.CR4和弹簧4.2.0.RELEASE

4

2 回答 2

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 Sc​​hemaExport / 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;

在此处查看更多详细信息 为什么 Hibernate Tools hbm2ddl 生成不考虑 Bean Validation 注释?

于 2016-01-10T03:14:02.587 回答
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);
}
于 2017-02-03T19:00:53.297 回答