2015-04-23 45 views
1

我已經使用maven配置了多模塊項目,因此我正在構建將在整個項目中使用的通用模塊。 現在的問題是我有我的Category.orm.xml文件在META-INF/domain/orm目錄中時,當我試圖從它拋出以下錯誤讀取命名查詢BC_READ_ALL_CATEGORIES無法讀取多個模塊項目中的orm.xml jpa

雖然如果我試圖從註釋中讀取相同的命名查詢,那麼沒有發生問題。

請參閱我在哪裏弄錯感謝...

Exception in thread "main" java.lang.IllegalArgumentException: No query defined for that name [BC_READ_ALL_CATEGORIES] 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.buildQueryFromName(AbstractEntityManagerImpl.java:788) 
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.createNamedQuery(AbstractEntityManagerImpl.java:925) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291) 
at com.sun.proxy.$Proxy36.createNamedQuery(Unknown Source) 
at com.ornamentbazzar.common.catalog.dao.CategoryDaoImpl.readAllCategories(CategoryDaoImpl.java:53) 

目錄結構

enter image description here

這是一個ApplicationContext-persistent.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:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.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/tx 
      http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <context:component-scan 
     base-package="com.ornamentbazzar.*" /> 
    <!-- this is also used we can used this also --> 
    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
     <property name="url" value="jdbc:mysql://localhost:3306/retailer" /> 
     <property name="username" value="root" /> 
     <property name="password" value="root" /> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 
    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceXmlLocation" value="classpath:* META-INF/persistence.xml" /> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="persistenceUnitName" value="ornament" /> 
     <property name="packagesToScan" value="com.ornamentbazzar.*" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /> 
      </bean> 
     </property> 
    </bean> 


    <bean id="category" class="com.ornamentbazzar.common.catalog.dao.CategoryDaoImpl"></bean> 


</beans>  

persistent.xml

<?xml version="1.0" encoding="UTF-8"?> 

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" 
    version="2.0"> 


    <persistence-unit name="ornament" transaction-type="RESOURCE_LOCAL"> 
     <mapping-file>META-INF/domain/orm/Category.orm.xml</mapping-file> 
     <!-- <mapping-file>META-INF/product.orm.xml</mapping-file> --> 
     <class>com.ornamentbazzar.common.catalog.entity.Category</class> 
     <class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class> 
     <class>com.ornamentbazzar.common.catalog.entity.CategoryMapperPK</class> 
     <class>com.ornamentbazzar.common.catalog.entity.CategoryAttribute</class> 
     <class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class> 
     <properties> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property> 
      <property name="hibernate.transaction.flush_before_completion" 
       value="false" /> 
      <property name="hibernate.connection.autocommit" value="true" /> 
      <property name="hibernate.cache.region.factory_class" 
       value="org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory" /> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" /> 
      <property name="hibernate.id.new_generator_mappings" value="true" /> 
      <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 
     </properties> 
    </persistence-unit> 
</persistence> 

category.orm.xml

<?xml version="1.0" encoding="UTF-8"?> 
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd" 
    version="2.0"> 

    <named-query name="BC_READ_ALL_CATEGORIES"> 
     <query>SELECT category FROM com.ornamentbazzar.common.catalog.entity.Category category ORDER BY category.id</query> 
    </named-query> 


</entity-mappings> 

TestApplication

public class Test { 
    public static void main(String[] args) { 
     ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
       "applicationContext-persistence.xml"); 
     CategoryDao categoryDao = (CategoryDao) applicationContext.getBean("category"); 
     System.out.println(categoryDao.readAllCategories().size()); 
    } 
} 

回答

0

persistence.xml只指單個*.orm.xml文件。要閱讀多個,你需要列出多個。你的persistence.xml只是指一個。你有多個地方?

<mapping-file>META-INF/domain/orm/Category.orm.xml</mapping-file> 
    <!-- <mapping-file>META-INF/product.orm.xml</mapping-file> --> 
+0

AMNET是的,我知道這一點,但問題是,我的測試程序本身無法讀取* .orm.xml命名查詢我不能夠弄清楚這是爲什麼呢,這是無法讀取 – henrycharles

+0

AMNET是的,我確實有多個orm文件,但首先我試圖用一個orm文件來測試名稱查詢是否正在讀取或不是問題,即使它不是用一個orm文件讀取。 – henrycharles