Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Hibernate: LocalSessionFactoryBean - NoSuchMethodError: org.hibernate.cfg.annotations.reflection.XMLContext

I am trying integrate Hibernate 4.0.0.FINAL with Spring 3.1.0.RELEASE using @Configuration.

Afterwards, this problem occurs:

BeanCreationException: Error creating bean with name 'alertsSessionFactoryBean'
NoSuchMethodError: org.hibernate.cfg.annotations.reflection.XMLContext$Default.getDelimitedIdentifier()Ljava/lang/Boolean;

This is my PersistenceHibernateConfig file

@Configuration
@EnableTransactionManagement
public class PersistenceHibernateConfig {

    @Value("${jdbc.driverClassName}")
    private String driverClassName;

    @Value("${jdbc.url}")
    private String url;

    @Value("${hibernate.dialect}")
    String hibernateDialect;

    @Value("${hibernate.show_sql}")
    boolean hibernateShowSql;

    @Value("${hibernate.hbm2ddl.auto}")
    String hibernateHbm2ddlAuto;

    @Bean
    public LocalSessionFactoryBean alertsSessionFactoryBean() {
        final LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(this.restDataSource());
        sessionFactory.setPackagesToScan(new String[]{"com.cloudlb"});
        sessionFactory.setHibernateProperties(this.hibernateProperties());
    
        return sessionFactory;
    }

    @Bean
    public DataSource restDataSource() {
        final DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(this.driverClassName);
        dataSource.setUrl(this.url);
        dataSource.setUsername("test");
        dataSource.setPassword("1234");

        return dataSource;
    }

    @Bean
    public HibernateTransactionManager transactionManager() {
        final HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(this.alertsSessionFactoryBean().getObject());

        return txManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslationPostProcessor() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    @Bean
    public PersistenceExceptionTranslator exceptionTranslator() {
        return new HibernateExceptionTranslator();
    }

    final Properties hibernateProperties() {
        return new Properties() {
            {
                this.put("persistence.dialect", PersistenceHibernateConfig.this.hibernateDialect);
                this.put("hibernate.hbm2ddl.auto", PersistenceHibernateConfig.this.hibernateHbm2ddlAuto);
                this.put("hibernate.show_sql", PersistenceHibernateConfig.this.hibernateShowSql);
            }
        };
    }
}

I think it could be a problem with LocalSessionFactoryBean but I can't understand what is wrong. I may be missing something.

I found out that it is because of missing hibernate-annotation.jar if it is 3.x Don't know why in 4.0 the annotation: org.hibernate.cfg.annotations.reflection.XMLContext is in hibernate-core jar file and it still error.

like image 901
xyzxyz442 Avatar asked Dec 26 '11 03:12

xyzxyz442


1 Answers

OK This sounds a lot like you're having issues providing compatible versions of all of the required dependencies. Here are a few thoughts on what could be wrong:

  1. It doesn't sound like you're using Maven or similar to manage your dependencies. Using an automatic tool to manage your dependencies is strongly recommended since it's very hard/error prone to provide all of the required dependencies manually. That said you should be able to download the release in a zip file containing all the required jars from here. Is this what you did?

  2. Hibernate core depends on hibernate-commons-annotations.jar. All the annotations which were in hibernate-annotations have been in the core jar for a while now. So you need hibernate-commons-annotations.jar not hibernate-annotations.jar

like image 55
Alex Barnes Avatar answered Oct 15 '22 12:10

Alex Barnes