0

I am unable to get spring to create tables automatically. I tried every solution I could find regarding the properties and annotation that should be added.

Spring version - 5.2.5.RELEASE

My application properties looks like that:


spring.datasource.url=jdbc:mysql://localhost:3306/ci_test?useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=****
spring.datasource.password=****

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.jpa.database-platform=org.hibernate.dialect.SQLServerDialect
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update

management.endpoints.web.exposure.include=*
management.endpoint.shutdown.enabled=true
#management.server.port=8090
logging.level.org.springframework=INFO

My Main app:

com.mypackage.autoTester;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.modelmapper.ModelMapper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.SpringVersion;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties
@EntityScan(basePackages = {"com.mypackage.autoTester"})  // scan JPA entities
public class AutoTesterApplication {

    public static final Logger logger = LogManager.getLogger();

    @Bean
    public ModelMapper modelMapper() {
        return new ModelMapper();
    }

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

    }

}

My Entity:

com.mypackage.autoTester.entities;

import lombok.*;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "testTable")
@Getter
@Setter
@AllArgsConstructor(access = AccessLevel.PUBLIC)
@NoArgsConstructor
public class TestEntity {

    @Id
    private long id;
}

Is there something wrong with the code above? Am I missing something?

UPDATE:

I tried the solutions below but no help :( Back in the days I used spring boot plugin in eclipse and it worked, I now work with intellij so I am not sure if it is related

Gilad Dahan
  • 508
  • 5
  • 19

3 Answers3

0

You need to update the ddl-auto to create instead of update in your application.properties

spring.jpa.hibernate.ddl-auto=create

Some of the values it accepts are:

create: creates the schema.

update: updates the schema.

validate: validates the schema.

create-drop: creates the schema and destroys the data previously present.

But this is not recommended for production databases, you can use flyway if you want incremental changes to be made to your db instead of hibernate update.

deepakchethan
  • 5,240
  • 1
  • 23
  • 33
  • From what I understand here is when I need a new schema, I go to properties and change it to create, and then when I want to update it I need to change it to update again? – Gilad Dahan Jun 29 '21 at 14:07
  • Also, it didn't work :( I am using Intellij if that makes any difference – Gilad Dahan Jun 29 '21 at 14:20
  • Do you see any errors in the logs of the app? – deepakchethan Jun 29 '21 at 14:32
  • Nope, all looks good when I run the app. I see the Entity I created under the JPA persistence tool if that helps – Gilad Dahan Jun 29 '21 at 14:34
  • I don't see any create statements but I added some more properties listed in https://stackoverflow.com/questions/30118683/how-to-log-sql-statements-in-spring-boot and I saw some select statements on exiting tables, but no create statements – Gilad Dahan Jun 29 '21 at 14:44
0

try to add to your application.properties :

spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
rbm
  • 57
  • 9
0

I found the solution and perhaps my question was lacking some information so it seems.

We are using 2 DBs and we initialize them manually in our code with the help of @EnableJpaRepositories

@Configuration
@EnableJpaRepositories(basePackages = "",
        entityManagerFactoryRef = "primaryEntityManager",
        transactionManagerRef = "primaryTransactionManager")
public class PrimaryConfig {
    private final Environment env;

    public PrimaryConfig(Environment env) {
        this.env = env;
    }

    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource primaryDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(env.getProperty("spring.datasource.url"));
        dataSource.setUsername(env.getProperty("spring.datasource.username"));
        dataSource.setPassword(env.getProperty("spring.datasource.password"));
        dataSource.setDriverClassName(Objects.requireNonNull(env.getProperty("spring.datasource.driver-class-name")));
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager primaryTransactionManager() {
        JpaTransactionManager transactionManager
                = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(
                primaryEntityManager().getObject());
        return transactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean primaryEntityManager() {
        final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
        em.setDataSource(primaryDataSource());
        em.setPackagesToScan("");

        final HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(true); // -> added this line 
        em.setJpaVendorAdapter(vendorAdapter);

        return em;
    }
}

So I needed to add only one line for it to work and start executing the SQL commands automatically.

It appears that since we created it manually, the properties added to the application.properties file didn't make any difference, or at least some of them.

I hope it will help other people who see this also since I wasted a lot of time on this :)

Gilad Dahan
  • 508
  • 5
  • 19