无法从org.springframework.orm.hibernate5.LocalSessionFactoryBean实例化sessionFactory

时间:2018-06-26 11:43:42

标签: java spring hibernate

我们在应用程序中使用spring和hibernate,我先创建LocalSessionFactoryBean对象,然后在存储库中创建@autowire SessionFactory,如下所示:在AppConfig中创建LocalSessionFactoryBean

 @EnableCaching
 @EnableAsync
 @EnableTransactionManagement
 @Configuration
 class AppCOnfig extends WebMvcConfigurerAdapter{
 @Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(restDataSource());
    sessionFactory.setPackagesToScan("com.xx.yyy");
    sessionFactory.setHibernateProperties(hibernateProperties());
    sessionFactory.setPhysicalNamingStrategy(new SnakeCaseNamingStrategy());
    sessionFactory.setImplicitNamingStrategy(new ImplicitNamingStrategyLegacyHbmImpl());
    return sessionFactory;
  }
}

在存储库中,我们将@autowire注释用作

@Slf4j
@Repository
public class UserRepository {

    @Autowired
    private SessionFactory sessionFactory;

}

我们正在使用以下的休眠和春季版本:   4.3.4发布   5.2.10最终版 但是,当我启动应用程序时,出现此错误:

Error creating bean with name 'sessionFactory' defined in XXX.AppConfig: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.hibernate5.LocalSessionFactoryBean]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.ArrayStoreException: sun.reflect.annotation.EnumConstantNotPresentExceptionProxy
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1022)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:512)

我知道spring尝试从localeSessionFactoryBean获取会话工厂时会遇到一些问题,但无法弄清楚我们如何解决它。如果有人对此有任何想法请帮助我。

1 个答案:

答案 0 :(得分:0)

当某个枚举常量不存在时试图通过名称引用它时,会抛出一个 BuildLayout。根据我的经验,当涉及反射时,会抛出它的 EnumConstantNotPresentException 变体。如果您在 Proxy 构造函数上使用断点调试您的应用,您应该能够看到枚举的类和不存在的常量的名称。