1

I have configured multi module project using maven ,so I am building the common module which is going to use across the project. Now the problem is I have my Category.orm.xml file inside META-INF/domain/orm directory when i tried to read the named query BC_READ_ALL_CATEGORIES from this its throwing following error.

While if I tried to read the same named query from annotation then there is no problem occurred.

Please see where i am mistaking THANKS...

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)

Directory Structure

enter image description here

This is the 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());
    }
}
henrycharles
  • 1,019
  • 6
  • 28
  • 66

1 Answers1

0

Your persistence.xml only refers to a single *.orm.xml file. To read multiple, you need to list multiple.

Your persistence.xml is only referring to one. Do you have multiple somewhere?

    <mapping-file>META-INF/domain/orm/Category.orm.xml</mapping-file>
    <!-- <mapping-file>META-INF/product.orm.xml</mapping-file> -->
John Ament
  • 11,595
  • 1
  • 36
  • 45
  • Amnet Yes I know that , but the problem is that, My Test program itself not able to read *.orm.xml named query i am not able to figure out this why this is not able to read – henrycharles Apr 23 '15 at 13:24
  • Amnet Yes i do have multiple orm file but first i am trying to test with one orm file wether the name query is reading or not problem is its not reading with one orm file even. – henrycharles Apr 24 '15 at 06:08