Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hibernate不自动创建表_Java_Spring_Hibernate_Spring Mvc - Fatal编程技术网

Java Hibernate不自动创建表

Java Hibernate不自动创建表,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我已经为Mysql配置了Spring和Hibernate,但Hibernate并没有自动创建表。我已经在eclipse数据源连接中配置了Mysql,并尝试使用tomcat 8和tomcat 9 我已将数据库属性配置为配置hibernate 请在下面查找代码 数据库.属性 mysql.driver=com.mysql.cj.jdbc.Driver mysql.url=jdbc:mysql://localhost:3306/bookdb mysql.user=root mysql.password=

我已经为Mysql配置了Spring和Hibernate,但Hibernate并没有自动创建表。我已经在eclipse数据源连接中配置了Mysql,并尝试使用tomcat 8和tomcat 9

我已将数据库属性配置为配置hibernate

请在下面查找代码

数据库.属性

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;
       }
    }
    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 {

    }
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[] { "/" };
       }
    }
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;
   }

}

问题是
AppConfig
类位于包
com.bushansirgur.config
中,它不在
@ComponentScan
下,而且我确实看到不同类上有多个
@ComponentScan
注释,这是不推荐的。您只需在带有基本包的
Main
类上使用一次即可

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

}

你能用hibernate.hbm2ddl.auto=create检查一下吗?谢谢你的快速回复,我会尝试一下你的建议。我能在AppConfig中添加@ComponentScan(“com.bushansirgur.config”)吗?