【问题标题】:Spring 4 with hibernate 5.2 Configuration( moving hibernate 4 to 5.2) : Failed to start the tomcat serverSpring 4 with hibernate 5.2 Configuration(将hibernate 4移动到5.2):无法启动tomcat服务器
【发布时间】:2021-10-21 11:18:15
【问题描述】:

以下是此 Hibernate 5 + spring 4 集成的错误日志。

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackages = { "com.social.*","com.*" })
public class HBConfig {

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

      Properties props = new Properties();
      // Setting JDBC properties
      props.put(DRIVER, "com.mysql.cj.jdbc.Driver");
      props.put(URL, "jdbc:mysql://localhost:3306/socialdev");
      props.put(USER, "root");
      props.put(PASS,"123");

      // Setting Hibernate properties
      props.put(SHOW_SQL, true);
      props.put(HBM2DDL_AUTO, "update");

      // Setting C3P0 properties
      props.put(C3P0_MIN_SIZE, 5);
      props.put(C3P0_MAX_SIZE, 20);
      props.put(C3P0_ACQUIRE_INCREMENT, 1);
      props.put(C3P0_TIMEOUT, 1800);
      props.put(C3P0_MAX_STATEMENTS, 150);

      factoryBean.setHibernateProperties(props);
      factoryBean.setPackagesToScan("com.social.entity");

      return factoryBean;
   }

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

以上是配置。让我知道我在做什么错误,使用相同的代码库 hibernate 4 对我来说工作正常。

【问题讨论】:

  • 请编辑您的问题并删除您的错误日志屏幕截图并将错误日志复制到您的问题中。

标签: java spring spring-boot hibernate tomcat


【解决方案1】:

这可能是版本不匹配问题,我在升级到 Hibernate 5.2 时遇到过这个问题。

事实上,stackoverflow 上有多个线程,但我找不到确切的解决方案,直到我发现 Spring 记录了支持 Hibernate 5.2 的问题,您可以在 https://github.com/spring-projects/spring-framework/issues/18899 参考该问题

就解决方案而言

  • 将你的 spring 版本升级到最新的 4.3.x 系列,从今天起你可以迁移到 Spring 4.3.30.RELEASE
  • 既然您已经迁移到 Hibernate 5.2,请确保所有 Hibernate 工件版本都是 5.2.x

这应该可以解决您的问题,

【讨论】:

    【解决方案2】:
      @Bean
      public DataSource dataSource() {
        return DataSourceBuilder.create().build();
      }
    
    
      @Bean
      @Primary
      public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;
      }
    
      @Bean
      @Primary
      public EntityManagerFactory entityManagerFactory(DataSource dataSource,
        JpaVendorAdapter jpaVendorAdapter) {
        LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
        lef.setDataSource(dataSource);
        lef.setJpaVendorAdapter(jpaVendorAdapter);
        Properties props = new Properties();
      props.put(DRIVER, "com.mysql.cj.jdbc.Driver");
      props.put(URL, "jdbc:mysql://localhost:3306/socialdev");
      props.put(USER, "root");
      props.put(PASS,"123");
    
      // Setting Hibernate properties
      props.put(SHOW_SQL, true);
      props.put(HBM2DDL_AUTO, "update");
    
      // Setting C3P0 properties
      props.put(C3P0_MIN_SIZE, 5);
      props.put(C3P0_MAX_SIZE, 20);
      props.put(C3P0_ACQUIRE_INCREMENT, 1);
      props.put(C3P0_TIMEOUT, 1800);
      props.put(C3P0_MAX_STATEMENTS, 150);
    
        props.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
        lef.setJpaProperties(jpaProperties);
        //lef.setPersistenceProvider(new HackedHibernatePersistence());
        lef.setPackagesToScan("com.social.entity");
        lef.afterPropertiesSet();
        
        return lef.getObject();
      }
    

    【讨论】:

    • 我正在使用 spring4 ,上面的配置似乎是启动的一部分。你能建议我春季 4 的解决方案吗?
    • 预期的原因是什么?
    猜你喜欢
    • 2016-10-05
    • 1970-01-01
    • 2021-09-25
    • 2014-12-18
    • 2018-07-02
    • 1970-01-01
    • 2019-09-24
    • 1970-01-01
    • 2012-11-18
    相关资源
    最近更新 更多