Spring JPA spring.jpa.hibernate.ddl-auto没有影响

时间:2017-08-08 12:23:37

标签: java spring hibernate spring-data-jpa

我必须有配置问题,但是当我更改spring.jpa.hibernate.ddl-auto的值时,它总是具有相同的行为,即如果它们不存在则创建表,如果我在我的实体中添加一个字段,则添加一个列,但永远不要删除或删除列,具有值的事件"创建"。

这与字段spring.jpa.generate-ddl = true。请注意,如果我将此字段更改为false,则会将其考虑在内。

这是我的配置文件:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
        entityManagerFactoryRef = "myEntityManagerFactory", 
        transactionManagerRef = "myTransactionManager",
        basePackages = {"com.myapp.repositories"})
public class MyConfig {
    @Primary
    @Bean(name = "myDataSource")
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource myDataSource() {
        return DataSourceBuilder.create().build();
    }
    @Primary
    @Bean(name = "myEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean myEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("orcaDataSource") DataSource myDataSource) {
        return builder.dataSource(orcaDataSource)
                .packages("com.myApp.model")
                .persistenceUnit("myapp")
                .build();
    }
    @Primary
    @Bean(name = "myTransactionManager")
    public PlatformTransactionManager myTransactionManager(
            @Qualifier("myEntityManagerFactory") EntityManagerFactory myEntityManagerFactory) {
        return new JpaTransactionManager(myEntityManagerFactory);
    }

我得到的跟踪(没有错误,但似乎总是更新,即使我处于验证模式)

[localhost-startStop-1] INFO org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 4894 ms
[localhost-startStop-1] INFO org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'myapp'
[localhost-startStop-1] INFO org.hibernate.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [
    name: myapp
    ...]
[localhost-startStop-1] INFO org.hibernate.Version - HHH000412: Hibernate Core {5.0.12.Final}
[localhost-startStop-1] INFO org.hibernate.cfg.Environment - HHH000206: hibernate.properties not found
[localhost-startStop-1] INFO org.hibernate.cfg.Environment - HHH000021: Bytecode provider name : javassist
[localhost-startStop-1] INFO org.hibernate.annotations.common.Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
[localhost-startStop-1] INFO org.hibernate.dialect.Dialect - HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
[localhost-startStop-1] INFO org.hibernate.engine.jdbc.env.internal.LobCreatorBuilderImpl - HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
[localhost-startStop-1] INFO org.hibernate.type.BasicTypeRegistry - HHH000270: Type registration [java.util.UUID] overrides previous : org.hibernate.type.UUIDBinaryType@7b79660
[localhost-startStop-1] INFO org.hibernate.tool.hbm2ddl.SchemaUpdate - HHH000228: Running hbm2ddl schema update
[localhost-startStop-1] INFO org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl - HHH000262: Table not found: BusinessArea
[localhost-startStop-1] INFO org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl - HHH000262: Table not found: BusinessDomain .... and so on for all my tables

1 个答案:

答案 0 :(得分:0)

好的我觉得我发现了...... 我修改了我的conf类:

    @Primary
    @Bean(name = "myEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean myEntityManagerFactory(
            EntityManagerFactoryBuilder builder,
            @Qualifier("myDataSource") DataSource myDataSource) {
    Properties prop = new Properties();
    prop.setProperty("hibernate.hbm2ddl.auto", env.getProperty("spring.jpa.hibernate.ddl-auto"));
    LocalContainerEntityManagerFactoryBean bean = builder.dataSource(myDataSource)
            .packages("com.myapp.model")
            .persistenceUnit("myapp")
            .build();
    bean.setJpaProperties(prop);
    return bean;
}

我还是不知道spring.jpa.hibernate.ddl-auto本身在做什么......