我正在使用与Spring注释集成的hibernate并为我的Entity类获取FileNotFoundException
这是我的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>
我的Dao课
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;
}
}
我的主要课程
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);
}
我收到错误
**引起:java.io.FileNotFoundException:无法打开类路径资源[vendors.Vendor],因为它不存在
答案 0 :(得分:0)
属性mappingResources
用于XML(hbm.xml
)映射。
Hibernae 4及以上
您只需要将所有持久化类放到some.package.model
包并使用
<property name="packagesToScan" value="some.package.model" />
添加该包中的所有类。内部包装也被处理。
您还可以使用
分别添加持久化类<property name="annotatedClasses">
<list>
<value>vendors.Vendor</value>
<value>accountBooks.DayBookData</value>
</list>
</property>
Hibernate 3
Hibearnate 3配置使用带注释的持久化类和Spring有点复杂。
需要向hibernate.cfg.xml
添加持久化类并通过这种方式配置Spring:
<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对带注释的配置使用单独的配置类:
org.hibernate.cfg.AnnotationConfiguration