BeanCreationException after restart computer/IntelliJ

时间:2018-03-23 00:33:07

标签: spring hibernate intellij-idea

I'm currently learning Spring & Hibernate with IntelliJ, I found the following error message after I reboot my computer even the code is working totally fine before the reboot, Could you please give me any hints to solve that.

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'categoryDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.hibernate.SessionFactory com.teamtreehouse.giflib.dao.CategoryDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [com/teamtreehouse/giflib/config/DataConfig.class]: Invocation of init method failed; nested exception is org.hibernate.tool.schema.spi.SchemaManagementException: Unable to obtain JDBC Connection

I created a configuration looks like this, and the categoryDaoImpl is using this sessionFactory,

@Configuration
@PropertySource("app.properties")

public class DataConfig {
    @Autowired
    private Environment env;

@Bean
public LocalSessionFactoryBean sessionFactory(){
    Resource config = new ClassPathResource("hibernate.cfg.xml");
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setConfigLocation(config);
    sessionFactory.setPackagesToScan(env.getProperty("giflib.entity.package"));     //Externalization
    sessionFactory.setDataSource(dataSource());
    return sessionFactory;
}

@Bean
public DataSource dataSource() {
    BasicDataSource ds = new BasicDataSource();

    //Set driver class name
    ds.setDriverClassName(env.getProperty("giflib.db.driver"));

    //Set URL
    ds.setUrl(env.getProperty("giflib.db.url"));

    //Set username & password
    ds.setUsername(env.getProperty("giflib.db.username"));
    ds.setPassword(env.getProperty("giflib.db.password"));

    return ds;
}

}

and the classpath resource,

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <!-- Set naming strategy -->
    <property name="hibernate.implicit_naming_strategy">org.hibernate.boot.model.naming.ImplicitNamingStrategyJpaCompliantImpl</property>

    <!-- Update the database schema (structure) on startup -->
    <property name="hbm2ddl.auto">create</property>
</session-factory>

app.properties

# Hash salt for shortened URLs
giflib.hash.salt = xOBtdmJZxRcz^jkkyHfkrkT1*02bJUn+YQts0*xCeka%cGHCN1fjaC*faFtY

#Package where our entities (models) are located
giflib.entity.package = com.teamtreehouse.giflib.model

#Details for our datasource
giflib.db.driver = org.h2.Driver
giflib.db.url = jdbc:h2:tcp://localhost/~/data/test
giflib.db.username = sa
giflib.db.password =

1 个答案:

答案 0 :(得分:0)

你必须在内存中有一些工作的H2实例。该错误告诉您无法获得数据库连接。我想你可能会覆盖你的数据源连接属性。如果您使用的是springboot,则无需定义sessionFactoy或dataSource bean。如果你不使用springboot,你应该是,除非你得到报酬,否则不需要学习旧东西。您也不需要xml hibernate-configuration属性文件。

尝试使用您的application.properties文件。
注意:
这是一个application.yml示例,但您可以使用每个yaml属性的路径转换为application.properties。查找yaml,这对于属性来说很好,也是一件好事。

# ===============================
# = DATA SOURCE
# ===============================
# Set here configurations for the database connection
spring:
  datasource:
# h2 in memory database will be destroyed on app shutdown
    url: jdbc:h2:mem:YOUR_DATABASE_NAME;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    platform: h2
    driverClassName: org.h2.Driver
    username: sa
    password:
# Keep the connection alive if idle for a long time (needed in production)
    testWhileIdle: true
    validationQuery: SELECT 1
# ===============================
# = JPA / HIBERNATE
# ===============================
# Show or not log for each sql query
  jpa:
    show-sql: true
# Hibernate ddl auto (create, create-drop, update): with "update" the database
# schema will be automatically updated accordingly to java entities found
    hibernate:
      ddl-auto: create
# Allows Hibernate to generate SQL optimized for a particular DBMS
      properties:
        hibernate:
          dialect: org.hibernate.dialect.H2Dialect
          use_sql_comments: true
          format_sql: true

添加JPA @Configuration类,但您不需要定义任何bean,springboot会为您执行此操作。

@Configuration
@EnableTransactionManagement
@ComponentScan({"com.teamtreehouse.giflib.model"})
@EnableJpaRepositories("ccom.teamtreehouse.giflib.repository")
public class JpaConfig {
   // You don't need to do any thing here atm.
}