2

I have configured Spring and Hibernate for Mysql but hibernate is not creating tables automatically.i have configured mysql in eclipse datasource connection and tried with tomcat 8 and tomcat 9

I have Configured Database properties to configure hibernate

please find the code below

Database.properties

mysql.driver=com.mysql.cj.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/bookdb
mysql.user=root
mysql.password=root

# Hibernate properties
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update

#C3P0 properties
hibernate.c3p0.min_size=5
hibernate.c3p0.max_size=20
hibernate.c3p0.acquire_increment=1
hibernate.c3p0.timeout=1800
hibernate.c3p0.max_statements=150

AppConfig

    package com.bushansirgur.config;

    import java.util.Properties;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.ComponentScans;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import org.springframework.orm.hibernate5.HibernateTransactionManager;
    import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
    import org.springframework.transaction.annotation.EnableTransactionManagement;
    import static org.hibernate.cfg.Environment.*;

    @Configuration
    @PropertySource("classpath:db.properties")
    @EnableTransactionManagement
    @ComponentScans(value = { @ComponentScan("com.bushansirgur.dao"),
          @ComponentScan("com.bushansirgur.service") })
    public class AppConfig {

       @Autowired
       private Environment env;

       @Bean
       public LocalSessionFactoryBean getSessionFactory() {
          LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

          Properties props = new Properties();
          // Setting JDBC properties
          props.put(DRIVER, env.getProperty("mysql.driver"));
          props.put(URL, env.getProperty("mysql.url"));
          props.put(USER, env.getProperty("mysql.user"));
          props.put(PASS, env.getProperty("mysql.password"));

          // Setting Hibernate properties
          props.put(SHOW_SQL, env.getProperty("hibernate.show_sql"));
          props.put(HBM2DDL_AUTO, env.getProperty("hibernate.hbm2ddl.auto"));

          // Setting C3P0 properties
          props.put(C3P0_MIN_SIZE, env.getProperty("hibernate.c3p0.min_size"));
          props.put(C3P0_MAX_SIZE, env.getProperty("hibernate.c3p0.max_size"));
          props.put(C3P0_ACQUIRE_INCREMENT, 
                env.getProperty("hibernate.c3p0.acquire_increment"));
          props.put(C3P0_TIMEOUT, env.getProperty("hibernate.c3p0.timeout"));
          props.put(C3P0_MAX_STATEMENTS, env.getProperty("hibernate.c3p0.max_statements"));

          factoryBean.setHibernateProperties(props);
          factoryBean.setPackagesToScan("com.bushansirgur.model");

          return factoryBean;
       }

       @Bean
       public HibernateTransactionManager getTransactionManager() {
          HibernateTransactionManager transactionManager = new HibernateTransactionManager();
          transactionManager.setSessionFactory(getSessionFactory().getObject());
          return transactionManager;
       }
    }

MyWebAppIntitalizer

    package com.bushansirgur.config;

    import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

    public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

       @Override
       protected Class<?>[] getRootConfigClasses() {
          return new Class[] { AppConfig.class };
       }

       @Override
       protected Class<?>[] getServletConfigClasses() {
          return new Class[] { WebConfig.class };
       }

       @Override
       protected String[] getServletMappings() {
          return new String[] { "/" };
       }
    }

WebConfig

    package com.bushansirgur.config;

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;

    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = { "com.bushansirgur.controller" })
    public class WebConfig extends WebMvcConfigurerAdapter {

    }

BookModel.java

package com.bushansirgur.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity(name = "Book")
public class Book {

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   private String title;
   private String author;

   public Long getId() {
      return id;
   }

   public void setId(Long id) {
      this.id = id;
   }

   public String getTitle() {
      return title;
   }

   public void setTitle(String title) {
      this.title = title;
   }

   public String getAuthor() {
      return author;
   }

   public void setAuthor(String author) {
      this.author = author;
   }

}
2
  • can you check with hibernate.hbm2ddl.auto=create ? Commented Jun 24, 2019 at 4:42
  • Thanks for quick reply i will give it a try Commented Jun 24, 2019 at 4:43

1 Answer 1

1

The problem is AppConfig class is in package com.bushansirgur.config which is not under the@ComponentScan, and also i do see multiple @ComponentScan annotations on different class which is not recommended. You can simply use it once on Main class with base package

 @Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.bushansirgur" })
public class WebConfig extends WebMvcConfigurerAdapter {

}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your suggestion .Can i add @ComponentScan("com.bushansirgur.config") in AppConfig?

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.