1

I'm using hibernate integrated with Spring annotation and getting FileNotFoundException for my Entity class

This is my applicationContext.xml

<?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.dbcp.BasicDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/db1"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <bean id="mysessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mappingResources">
            <list>
                <value>vendors.Vendor</value>
                <value>accountBooks.DayBookData</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="mysessionFactory"></property>
    </bean>

    <bean id="vendor" class="vendors.VendorDao">
        <property name="hibernateTemplate" ref="template"></property>
    </bean>

    <bean id="dayBook" class="accountBooks.DayBookDao">
        <property name="hibernateTemplate" ref="template"></property>
    </bean>

</beans>  

My Dao class

public class DayBookDao {

    private HibernateTemplate hibernateTemplate;

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

    public void saveDayBookData(DayBookData dayBook){
        hibernateTemplate.save(dayBook);
    }

    public void updateDayBookData(DayBookData dayBook){
        hibernateTemplate.update(dayBook);
    }

    public void deleteDayBookData(DayBookData dayBook){
        hibernateTemplate.delete(dayBook);
    }

    public List<DayBookData> getDayBookData(){
        List<DayBookData> dayBook = hibernateTemplate.loadAll(DayBookData.class);
        return dayBook;
    }
}

My Main class

public static void main(String[] args) {
        // TODO Auto-generated method stub

        Resource r = new ClassPathResource("applicationContext.xml");
        BeanFactory bean = new XmlBeanFactory(r);

        DayBookDao dayBookDao = (DayBookDao) bean.getBean("dayBook");
        DayBookData dayBook =  new DayBookData();
        dayBook.setAccountType("Bank Account");
        dayBook.setTransType("Receipt");
        dayBook.setOppAccount("Profit-Loss");
        dayBook.setAmount(15000);
        dayBook.setTransDate(new Date());
        dayBookDao.saveDayBookData(dayBook);
    }

I'm getting error

**Caused by: java.io.FileNotFoundException: class path resource [vendors.Vendor] cannot be opened because it does not exist

white_cat
  • 67
  • 1
  • 2
  • 7

1 Answers1

0

Property mappingResources are used for XML (hbm.xml) mapping.

Hibernae 4 and above

You just need to put all your persistent classes to the some.package.model package and use

<property name="packagesToScan" value="some.package.model" />

to add all classes from that package. Inner packages are processed as well.

Also you can add persistent classes separately using

<property name="annotatedClasses">
     <list>
       <value>vendors.Vendor</value>
       <value>accountBooks.DayBookData</value>
     </list>
 </property>

Hibernate 3

Hibearnate 3 configuration using annotated persistent classes and Spring a bit more complex. It is need to add persistent classes to the hibernate.cfg.xml and configure Spring by this way:

<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
  </property>

  <property name="configurationClass">
    <value>org.hibernate.cfg.AnnotationConfiguration</value>
  </property>
</bean>

Hibernate 3 uses a separate config class for configurations with annotations:

org.hibernate.cfg.AnnotationConfiguration
v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • It's throwing org.springframework.beans.NotWritablePropertyException: Invalid property 'packagesToScan' of bean class [org.springframework.orm.hibernate3.LocalSessionFactoryBean] – white_cat May 15 '17 at 10:25
  • @PoojaDave I think that you use an old `Spring` version, without this property. You can add classes one by one using `annotatedClasses`. http://stackoverflow.com/a/19746386/3405171 – v.ladynev May 15 '17 at 10:40
  • I'm using Spring 3.2 and for annotatedClasses also the same exception I'm getting. – white_cat May 15 '17 at 10:46
  • I tried using Spring 4.3.8 and now I'm getting java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory – white_cat May 15 '17 at 11:03
  • @PoojaDave You just need to use right jars. I updated my answer for Hibernate 3. – v.ladynev May 15 '17 at 11:19