我正在做一个Spring/Hibernate应用程序,使用注释来自动装载服务和存储库层并且配置较少。所有似乎成功的做法,因为当我运行应用程序时,一切似乎都适当地初始化。Hibernate映射资源无法打开,因为它不存在于Spring + Hibernate Maven项目中
问题
然而,当我在我的applicationContext-datasource.xml添加映射资源,我得到一个错误,指出了一个SessionFactory不能被正确初始化,因为它似乎可以找到我的pojo.hbm。 xml文件。请参阅:
造成的:java.io.FileNotFoundException:类路径的资源[COM/dariopardo/jfreechartdemo/POJO/Grid.hbm.xml]不能打开,因为它不存在
设置
的applicationContext-datasource.xml
该文件位于下的src /主/资源/ META_INF /弹簧
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:lang="http://www.springframework.org/schema/lang"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"
default-autowire="byName">
<context:annotation-config/>
<tx:annotation-driven/>
<context:component-scan base-package="com.dariopardo.jfreechartdemo.service">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Service"/>
</context:component-scan>
<context:component-scan base-package="com.dariopardo.jfreechartdemo.dao">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@myserverhere:1521:mydb" />
<property name="username" value="myusername" />
<property name="password" value="mypwd" />
</bean>
<!-- Hibernate SessionFactory for k12-->
<!--<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"><ref local="dataSource"/></property>
<property name="mappingResources">
<list>
<value>com/dariopardo/jfreechartdemo/pojo/Grid.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.bytecode.use_reflection_optimizer">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.region_prefix">hibernate.k12</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">false</prop>
<prop key="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</prop>
<prop key="hibernate.connection.pool_size">1</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
<prop key="hibernate.default_schema">K12INTEL_DW</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.jdbc.batch_versioned_data">true</prop>
<prop key="hibernate.jdbc.use_streams_for_binary">true</prop>
<prop key="hibernate.max_fetch_depth">1</prop>
<prop key="hibernate.proxool.pool_alias">pool_K12</prop>
<prop key="hibernate.query.substitutions">yes 'Y', no 'N'</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.transaction.auto_close_session"></prop>
<prop key="hibernate.transaction.flush_before_completion"></prop>
<prop key="hibernate.use_sql_comments">true</prop>
</props>
</property>
<property name="eventListeners">
<map>
<entry key="merge">
<bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener"/>
</entry>
</map>
</property>
</bean>
<!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/>
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml
classpath*:META-INF/spring/applicationContext*.xml
</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我的POJO
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.dariopardo.jfreechartdemo.pojo">
<class name="Grid">
<cache usage="read-only" />
<id name="c1"/>
<property name="c2"></property>
<property name="c3"></property>
</class>
<sql-query name="studentAttendanceOver95Pct">
<return alias="p" class="com.dariopardo.jfreechartdemo.pojo.Grid">
<return-property name="c1" column="school"/>
<return-property name="c2" column="at_risk"/>
<return-property name="c2" column="not_at_risk"/>
</return>
My custom sql query goes here
</sql-query>
我DAO层
@Repository
public class K12DaoImpl extends HibernateDaoSupport implements K12DaoManager{
@Autowired
public K12DaoImpl(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
@Override
public List<Grid> getAttendanceBySchoolOver95Pct() {
List<Grid> list = getHibernateTemplate().findByNamedQuery("studentAttendanceOver95Pct");
return list;
}
}
我的服务层
@Service
public class K12ManagerImpl implements K12Manager {
@Resource
private K12DaoManager k12DaoManager;
@Override
public List<Grid> studentPctAttendance() {
return k12DaoManager.getAttendanceBySchoolOver95Pct();
}
}
如果我评论了你hml.xml在mappingResources部分似乎一切都正确连接起来,但只要我在应用程序无法初始化的映射资源部分中引入任何hbm.xml文件。
有什么想法?
感谢, - 达里奥
**更新**我创建了一个文件夹strucutre com/dariopardo/jfreechartdemo/pojo,并将hbm.xml文件放在那里,它似乎工作。这是正确的方法吗? – Viriato