1

I'm new to spring. I.m learning spring from javapoint. After learning some basics of spring from javapoint and spring docs, I moved towards learning hibernate with spring, but in very first try (example), I stucked with the expception: NoClassDefFoundExpetion: javax/persistence/PersistenceContext. To resolve this exception, I've googled and looked for similar kind of situations (and their solutions) on this and this, but nothing helps me.
Here is the full stacktrace: of my exception

    Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.context.annotation.internalPersistenceAnnotationProcessor': Instantiation of bean failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/PersistenceContext
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1105)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1050)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
    at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:207)
    at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:697)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:526)
    at org.springframework.context.annotation.AnnotationConfigApplicationContext.<init>(AnnotationConfigApplicationContext.java:84)
    at test.Test.main(Test.java:15)
Caused by: java.lang.NoClassDefFoundError: javax/persistence/PersistenceContext
    at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.<clinit>(PersistenceAnnotationBeanPostProcessor.java:172)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1098)
    ... 12 more

I'm using eclipse-neon IDE, spring-framework 4.3.2 RELEASE, Hibernate-5.2.2 Final and Oracle 10G(database).There is a STUDENT table in my database having 4-5 entries. Also I've written thespring code using simple (console base) java project and NOT using any build tool. Here is my complete code and libraries list which I'm currently using:

Student.java

public class Student {

    private Integer rollNo;
    private String name;
    /**
     * @param rollNo
     * @param name
     */
    public Student(Integer rollNo, String name) {
        super();
        this.rollNo = rollNo;
        this.name = name;
    }

//Getter and setter ....

StudentDAO.java

public class StudentDAO {

    private HibernateTemplate hibernateTemplate;

    public StudentDAO(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;

    }

    public void setHibernateTemplate(HibernateTemplate template) {
        this.hibernateTemplate = template;
    }

    public void saveStudent(Student student) {
        hibernateTemplate.save(student);
    }

    public List<Student> readAll() {
        return hibernateTemplate.loadAll(Student.class);
    }
}

I'm using java annotation based configuration, So my AppConfig.java is:

@Configuration
public class AppConfig {

    @Bean
    public DriverManagerDataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource("jdbc:oracle:thin:@localhost:1521/xe", "user", "password");
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        return dataSource;
    }

    @Bean 
    public LocalSessionFactoryBean sessionFactoryBean() {
        LocalSessionFactoryBean bean = new LocalSessionFactoryBean();
        bean.setDataSource(dataSource());
        bean.setMappingResources("xml/student.hbm.xml");
        Properties prop = new Properties();
        prop.setProperty("hibernate.dialect", "org.hibernate.dialect.Oracle9Dialect");
        prop.setProperty("hibernate.hbm2ddl.auto", "update");
        prop.setProperty("hibernate.show_sql", "true");
        bean.setHibernateProperties(prop);
        return bean;
    }

    @Bean
    public HibernateTemplate hibernateTemplate() {
        HibernateTemplate template = new HibernateTemplate((SessionFactory) sessionFactoryBean());
        return template;
    }

    @Bean
    public StudentDAO DAO() {
        return new StudentDAO(hibernateTemplate());
    }

}

Student.hbm.xml XML file:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC  
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
                <class name="bean.Student" table="student">
                                <rollNo name="rollNo">
                                                <generator class="assigned"></generator>
                                </rollNo>
                                <property name="name"></property>
                </class>

</hibernate-mapping>

and here is my Main-Method Class

public class Test {

    public static void main(String[] args) {
            ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
            StudentDAO dao = context.getBean(StudentDAO.class);
            List<Student> list = dao.readAll();
            for(Student s: list) 
                System.out.println(s);
    }

}

here is the list of liberaries (with jar file) which I've included in my project:

SPRING

enter image description here

HIBERNATE

enter image description here

Commons logging and ORALCE 10G Driver
enter image description here

And some other too
enter image description here

How to solve the above mentioned exception, which library I've to add/remove or what else I can try to resolve the issue. Regret for long question

Example Reference
This example is based on source code, available at javapoint

Community
  • 1
  • 1
Sachin Kumar
  • 781
  • 1
  • 9
  • 28
  • u need to add annotations for Student.java..... for eg:@Entity.. – khaja firoz Aug 31 '16 at 11:59
  • What else entity I need to add. – Sachin Kumar Aug 31 '16 at 12:06
  • import package called javax.persistence package and from that package u need to declare above Class name..@Entity... – khaja firoz Aug 31 '16 at 12:09
  • I've imported javax.persistence package and also declare Student.java as entity class using @Entity annotation, but still the problem continues – Sachin Kumar Aug 31 '16 at 12:11
  • I'm already using hibernate-jpa-2.1-api-1.0.0.Final.jar which has higher version then mentioned.. – Sachin Kumar Aug 31 '16 at 12:37
  • hibernate-jpa provides javax.persistence, I think you don't need javax.persistence-2.1.0.jar. Try removing it, because you have different implementations of the same class in some jars – Tobías Aug 31 '16 at 13:01
  • @Tobías Thanks for suggestion, But I add javax.persistence later, when I was looking for solutions on net. I've tried it again, but all goes in vain – Sachin Kumar Aug 31 '16 at 17:38

2 Answers2

1

I made slightly changes in my example. Instead of AppConfig.java, I used ApplicationContext.xml. Here is code for file

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
                xsi:schemaLocation="http://www.springframework.org/schema/beans  
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


                <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
                                <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
                                <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe"></property>
                                <property name="username" value="sachin"></property>
                                <property name="password" value="sachin476"></property>
                </bean>

                <bean id="mysessionFactory"
                                class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
                                <property name="dataSource" ref="dataSource"></property>

                                <property name="mappingResources">
                                                <list>
                                                                <value>student.hbm.xml</value>
                                                </list>
                                </property>

                                <property name="hibernateProperties">
                                                <props>
                                                                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle9Dialect
                                                                </prop>
                                                                <prop key="hibernate.hbm2ddl.auto">update</prop>
                                                                <prop key="hibernate.show_sql">true</prop>

                                                </props>
                                </property>
                </bean>

                <bean id="template" class="org.springframework.orm.hibernate5.HibernateTemplate">
                                <property name="sessionFactory" ref="mysessionFactory"></property>
                </bean>

                <bean id="d" class="bean.dao.StudentDao">
                                <property name="template" ref="template"></property>
                </bean>
</beans>  

and added two more jar files:

>commons-dbcp2-2.1.1.jar
>commons-pool2-2.4.2.jar

for BasicDataSource class.

Now It gives me Exception:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [xml/ApplicationContext.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/hibernate/SessionFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:757)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at test.Test.main(Test.java:15)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/SessionFactory
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetPublicMethods(Class.java:2902)
    at java.lang.Class.getMethods(Class.java:1615)
    at org.springframework.beans.ExtendedBeanInfoFactory.supports(ExtendedBeanInfoFactory.java:54)
    at org.springframework.beans.ExtendedBeanInfoFactory.getBeanInfo(ExtendedBeanInfoFactory.java:46)
    at org.springframework.beans.CachedIntrospectionResults.<init>(CachedIntrospectionResults.java:270)
    at org.springframework.beans.CachedIntrospectionResults.forClass(CachedIntrospectionResults.java:189)
    at org.springframework.beans.BeanWrapperImpl.getCachedIntrospectionResults(BeanWrapperImpl.java:173)
    at org.springframework.beans.BeanWrapperImpl.getLocalPropertyHandler(BeanWrapperImpl.java:226)
    at org.springframework.beans.BeanWrapperImpl.getLocalPropertyHandler(BeanWrapperImpl.java:63)
    at org.springframework.beans.AbstractNestablePropertyAccessor.getPropertyHandler(AbstractNestablePropertyAccessor.java:725)
    at org.springframework.beans.AbstractNestablePropertyAccessor.isWritableProperty(AbstractNestablePropertyAccessor.java:557)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1483)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    ... 11 more

But I peeped into hibernate-core-5.2.2-Final.jar, that had definition for SessionFectory.class in org.hibernate package i.e there was class entry named org.hibernate.SessionFactory.class. Now I'm wondering why it gives such exceptions

UPDATE After lots of try and search on google, I found that hibernate 5 doesn't support oracle10g drivers. so I've updated ojdbc14.jar to ojdbc6.jar, and it works for me

Sachin Kumar
  • 781
  • 1
  • 9
  • 28
0

As mentioned here Upgrade your hibernate jpa-jar since the jpa supported version for hibernate4.3+ is 2.1.

Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52