Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java SE + Spring Data + Hibernate

I am trying to start a Java SE application with Spring Data + Hibernate and have done the follwowing till now:

Configuration File

@Configuration
@PropertySource("classpath:hibernate.properties")
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfiguration {

    private static final String PROPERTY_NAME_DATABASE_DRIVER = "db.driver";
    private static final String PROPERTY_NAME_DATABASE_PASSWORD = "db.password";
    private static final String PROPERTY_NAME_DATABASE_URL = "db.url";
    private static final String PROPERTY_NAME_DATABASE_USERNAME = "db.username";

    private static final String PROPERTY_NAME_HIBERNATE_DIALECT = "hibernate.dialect";
    private static final String PROPERTY_NAME_HIBERNATE_SHOW_SQL = "hibernate.show_sql";
    private static final String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN =
            "entitymanager.packages.to.scan";

    @Resource
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(
                env.getRequiredProperty(PROPERTY_NAME_DATABASE_DRIVER));
        dataSource.setUrl(env.getRequiredProperty(PROPERTY_NAME_DATABASE_URL));
        dataSource.setUsername(env.getRequiredProperty(PROPERTY_NAME_DATABASE_USERNAME));
        dataSource.setPassword(env.getRequiredProperty(PROPERTY_NAME_DATABASE_PASSWORD));

        return dataSource;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new
                LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource());
        entityManagerFactoryBean.setPersistenceProviderClass(org.hibernate
                .jpa.HibernatePersistenceProvider.class);
        entityManagerFactoryBean.setPackagesToScan(env.
                getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));

        entityManagerFactoryBean.setJpaProperties(hibProperties());

        return entityManagerFactoryBean;
    }

    private Properties hibProperties() {
        Properties properties = new Properties();
        properties.put(PROPERTY_NAME_HIBERNATE_DIALECT,
                env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_DIALECT));
        properties.put(PROPERTY_NAME_HIBERNATE_SHOW_SQL,
                env.getRequiredProperty(PROPERTY_NAME_HIBERNATE_SHOW_SQL));
        return properties;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
        return transactionManager;
    }

    @Bean
    public MainBean mainBean() {
        return new MainBean();
    }
}

Start class

public class Start {


    private static final String CONFIG_PACKAGE = "org.app.config";


    public static void main(String[] args) {

        try (AnnotationConfigApplicationContext ctx = new
                AnnotationConfigApplicationContext()) {

            ctx.scan(CONFIG_PACKAGE);
            ctx.refresh();

            MainBean bean = ctx.getBean(MainBean.class);
            bean.start();
        }
    }
}

Main Bean

public class MainBean {

        public void start() {

        System.out.println("Application Started. . .");


    }

}

However I'm getting the following exception

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: file [C:\MyPath\target\classes\org\app\config\JpaConfiguration.class]; nested exception is java.lang.IllegalArgumentException
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:281)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.doScan(ClassPathBeanDefinitionScanner.java:242)
    at org.springframework.context.annotation.ClassPathBeanDefinitionScanner.scan(ClassPathBeanDefinitionScanner.java:220)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.scan(AnnotationConfigApplicationContext.java:153)
    at org.app.Start.main(Start.java:25)
Caused by: java.lang.IllegalArgumentException
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.asm.ClassReader.<init>(Unknown Source)
    at org.springframework.core.type.classreading.SimpleMetadataReader.<init>(SimpleMetadataReader.java:52)
    at org.springframework.core.type.classreading.SimpleMetadataReaderFactory.getMetadataReader(SimpleMetadataReaderFactory.java:80)
    at org.springframework.core.type.classreading.CachingMetadataReaderFactory.getMetadataReader(CachingMetadataReaderFactory.java:101)
    at org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider.findCandidateComponents(ClassPathScanningCandidateComponentProvider.java:257)
    ... 4 more

Anyone have an idea what I'm doing wrong?

like image 805
ChrisGeo Avatar asked Mar 29 '14 11:03

ChrisGeo


People also ask

Can we use Hibernate in spring data?

With Spring Data, you may use Hibernate, EclipseLink, or any other JPA provider.

Which spring package is used for spring data JPA with Hibernate?

It is an ORM tool to persist java objects into the relational databases. JPA uses javax. persistence package. Hibernate uses org.

Can JPA work without Hibernate?

JPA can be used without a JPA provider aka Hibernate, EclipseLink and so on only if the application server has already a JPA implementation.

Can we use Hibernate with spring boot?

To test hibernate configuration with Spring boot, we need to autowire the EmployeeRepository dependency in a class and use it's method to save or fetch employee entities. Let's do this testing in @SpringBootApplication annotated class and using CommandLineRunner interface.


2 Answers

The exception you see is not very likely to be cause by invalid Spring configuration but your classpath setup. This feels like a version incompatibility regarding the ASM libraries and Java 8. The ASM version, Spring 3.2 depends on is known to be incompatible with Java 8.

Thus, if you want run your code with Java 8, you need to use a recent Spring 4.0 version.

like image 134
Oliver Drotbohm Avatar answered Sep 23 '22 01:09

Oliver Drotbohm


I was also facing same issue with Spring-3.2.0 and Java 8 ( jdk1.8.0_60 ). After using 3.2.14 release of spring framework , it works.

like image 28
Dheeraj Dhiman Avatar answered Sep 22 '22 01:09

Dheeraj Dhiman