How to disable DDL operation in spring boot. ddl should not apply as if we will provide the properties

Viewed 538

Can anyone please help with this? I have a requirement to set default values of properties spring.jpa.hibernate.ddl-auto=none and spring.jpa.generate-ddl=false from the application so even after changing from the properties file values should not change not perform DDL operation. Basically, I have to prevent DDL operation.

Thank you.

I have tried setting like this but still, these properties are applicable from yml. when I am setting values properties file it is performing DDL operation.

@SpringBootApplication
public class Main {
  public static void main(String[] args) {
    SpringApplication springApplication = new SpringApplication(Main.class);
    
     Map<String, Object> properties = new HashMap<>();
     properties.put("spring.jpa.hibernate.ddl-auto", "none");
     properties.put("spring.jpa.generate-ddl", false);
       
     springApplication.setDefaultProperties(properties);
     springApplication.run(args);
 }
}

YAML file

spring:
  jpa:
    generate-ddl: true
    hibernate:
      ddl-auto: create
2 Answers

Try this way,

Config your project like this, First add to pom.xml

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
  <version>2.3.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-core</artifactId>
  <version>5.4.32.Final</version>
</dependency>

Then make ApiApplication.java file to

@SpringBootApplication
public class ApiApplication {

    public static void main(String[] args) {
        SpringApplication.run(ApiApplication.class, args);
    }
}

Finally add for application.properties

spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=none

Then, I hope your problem will solve.

You can try to configure all your database/jpa options programmatically via a @Configuration class so this way changes would have to be applied via code. An example of such class is (setup below is for mysql, you can adapt it to yours), check the additionalProperties() method:

@Configuration
@EnableTransactionManagement
public class JpaConfig {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean em
                = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(dataSource());
        em.setPackagesToScan(new String[] { "package.of.your.jpaentities" });

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        em.setJpaVendorAdapter(vendorAdapter);
        em.setJpaProperties(additionalProperties());

        return em;
    }

    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername( "user" );
        dataSource.setPassword( "password" );
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

        return transactionManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties additionalProperties() {
        Properties properties = new Properties();
        properties.setProperty("hibernate.hbm2ddl.auto", "none");
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");

        return properties;
    }
}

also make sure to exclude spring-boot's autoconfiguration for databases:

@SpringBootApplication
@EnableAutoConfiguration (exclude = {  DataSourceAutoConfiguration.class })
public class Main {
    public static void main(String[] args) {
        SpringApplication springApplication = new SpringApplication(Main.class);
        springApplication.run(args);
    }
}
Related