0

Earlier I was able to create a table in MySQL using my program. But now it doesn't seem to be working. Can someone point out the issue out to me.

application.properties:

#jdbc
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/motsach
jdbc.username=root
jdbc.password=123456


#hibernate
hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
hibernate.hbm2ddl.auto = create
hibernate.current.session.context.class = org.springframework.orm.hibernate5.SpringSessionContext
hibernate.show_sql=true
hibernate.format_sql=true

Database Config file:

@Bean
public LocalSessionFactoryBean sessionFactory(DataSource dataSource) {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource);
    sessionFactory.setPackagesToScan(new String[] { "com.project.form.model" });
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

private Properties hibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
    properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
    properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
    properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
    properties.put("hibernate.current.session.context.class", environment.getRequiredProperty("hibernate.current.session.context.class"));        
    return properties;
}

@Bean(name ="dataSource")
public DataSource getDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
    dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
    dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
    dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
    System.out.println("## getDataSource: " + dataSource);
    return dataSource;
}
5
  • Change "hibernate.hbm2ddl.auto = create" into "hibernate.hbm2ddl.auto = update". OR its better to just use "spring.jpa.hibernate.ddl-auto = update". no need to add it in hibernateProperties() Commented Sep 19, 2017 at 7:15
  • Thank you, but nothing change. Commented Sep 19, 2017 at 7:17
  • create should work fine for dropping and creating db at every run Commented Sep 19, 2017 at 7:32
  • @D.Doe add spring.jpa.hibernate.ddl-auto = update". in application.properties file. no need to add it in hibernateProperties() Commented Sep 19, 2017 at 7:33
  • Thanks bro, but still error. Commented Sep 19, 2017 at 7:38

2 Answers 2

1

With this configuration you can create table automatically during spring boot application start.

server.contextPath=/demo-app
spring.datasource.url=jdbc:mysql://localhost:3306/testdb
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.implicit-strategy=org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyHbmImpl
spring.jpa.hibernate.naming.physical-strategy=org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
Sign up to request clarification or add additional context in comments.

Comments

0

I am using an application.yml file and this worked for me.

Refered link

# application.yml 
spring:
    datasource:
        driverClassName: com.mysql.jdbc.Driver
        password: abcd
        url: jdbc:mysql://${MYSQL_HOST:localhost}:3306/sonoo?autoReconnect=true&useSSL=false&createDatabaseIfNotExist=true
        username: root
    jpa:
        hibernate.ddl-auto: update
        generate-ddl: true
        show-sql: true

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.