hbm2ddl.auto更新

时间:2018-01-15 00:19:45

标签: java spring hibernate spring-mvc

@Configuration
@EnableTransactionManagement
@ComponentScans(value = {
            @ComponentScan("org.kalifornia.fridgeapp.DAO"),
            @ComponentScan("org.kalifornia.fridgeapp.service")
            } )
@PropertySource("classpath:mysql.properties")
public class HibernateConfig {

@Autowired
private ApplicationContext context;

@Autowired
private Environment env;

@Bean
public LocalSessionFactoryBean getSessionFactory()
{
    LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();
    try
    {
        factoryBean.setHibernateProperties(hibernateProperties());
        factoryBean.afterPropertiesSet();
        factoryBean.setDataSource(restDataSource());
        factoryBean.setAnnotatedClasses(FridgeUser.class);
        factoryBean.setAnnotatedClasses(Community.class);
        factoryBean.setAnnotatedClasses(Container.class);
        factoryBean.setAnnotatedClasses(Item.class);
        factoryBean.setAnnotatedClasses(Product.class);

    } catch(IOException exception) {
        System.out.print(exception.fillInStackTrace());
        return null;
    }
    return factoryBean;
}

@Bean
public DataSource restDataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("com.mysql.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/fridge");
    dataSource.setUsername("root");
    dataSource.setPassword("kaszanka");
    return dataSource;
}

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

Properties hibernateProperties() {
    return new Properties() {
        {
            setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
            setProperty("show_sql", "true");
            setProperty("hibernate.hbm2ddl.auto", "update");
        }
    };
}
}

我从教程中编写了该代码并且现在可以正常工作但是当我尝试将hbm2ddl.auto从创建更改为更新时,我收到此错误:

  

$线程中的异常" main" org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称' DAOImpl创建bean时出错:通过字段' sessionFactory'表达的不满意的依赖关系嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名为' getSessionFactory'的bean时出错在org.kalifornia.fridgeapp.spring.config.HibernateConfig中定义:调用init方法失败;嵌套异常是java.lang.UnsupportedOperationException:BasicDataSource不支持

1 个答案:

答案 0 :(得分:0)

根据您的日志nested exception is java.lang.UnsupportedOperationException: Not supported by BasicDataSource似乎是BasicDataSource的问题,请尝试将其更改为DriverManagerDataSource,因为可能是BasicDataSource不支持hibernate.hbm2ddl.auto = update