java.lang.IllegalArgumentException: Named query not found.(Entity Manager not creating NamedQuery)

左心房为你撑大大i 提交于 2019-12-05 22:09:48
I have written all named queries in a class with @NamedQuery annotation

You have not mentioned clearly the type of class that you are referring in the above statement? You would need to write named queries in Entity class (class annotated with @Entity annotation).

UPDATE: I am somewhat now confused about your class DBNamedQuery. You said you are using one class to put all named queries. My understanding is you are using this class to write named queries for all the entities of your application. If that is correct how can you use @Entity annotation on your class DBNamedQuery because it is not really a jpa entity?

A class that contains @NamedQuery annotation should be a managed entity. And I suspect your class DBNamedQuery is not.

To identify the problem I suggest check in the logs if that is a manged entity. If you cannot do that then EntityManger gives you and API to check that during runtime contains(java.lang.Object entity).

On a related note, if you are using annotation then for JPA Named Queries are part of jpa entity. Using xml gives you a flexibility to store in a separate file.

hi i got your problem instead of creating a class why not u create a xml file for named query like Queries.hbm.xml like as follow

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <!-- Query For User TO -->
    <query name="loadUserByUsername">from User u where u.username = ?</query>
</hibernate-mapping>

keep this xml file in resources folder and in your applicationContext-enterprise-config.xml file write it as follow

<property name="mappingResources">
    <list>
        <value>/resources/hbms/Queries.hbm.xml</value>
    </list>
</property>

so it will work fine.

Edit Well It's Depends how You Call it in your DAO Layer. You have to call like =>

public int executeNamedQuery(String namedQuery, String namedParams[], Object params[]) throws DatabaseException {
        int result = 0;
        Session session = getSession();
        final String methodName = "executeNamedQuery";
        try {
            session.beginTransaction();
            Query q = session.getNamedQuery(namedQuery);
            if (namedParams != null) {
                for (int i = 0; i < namedParams.length; i++) {
                    q.setParameter(namedParams[i], params[i]);
                }
            }
            result = q.executeUpdate();
            session.getTransaction().commit();
            logger.debug("{} :: {} = {}", new Object[] { methodName, "No. of objects affected", result });
        } catch (HibernateException e) {
            session.getTransaction().rollback();

            final String message = "Couldn't execute the named query " + namedQuery;
            logger.error("{} :: {}", new Object[] { methodName, message, e });
            throw new DatabaseException(getClass(), methodName, message, e);
        } finally {
            closeSession();
        }
        return result;
    }

Note: 1) namedQuery means name of query in Queries.hbm.xml file. 2) namedParams[] means parameters name for Query. 3) params means value for parameters .

Check if your entity class DBNamedQuery is in the package com.project.entities as you configured in the ApplicationContext.xml file

I think it is because of name miss match. The name which is declare in @NamedQuery in not match with the name which is called that query.

e.g

@NamedQuery(name = "AAAA", query = ... ) //must be same the name which call that query

public void some() {
   Query q = em.createNamedQuery("AAAA");
 }

Adding the annotation scanning in the persistence.xml solved the issue for me

<property name="hibernate.archive.autodetection" value="class, hbm" />

Even i tried giving the package scan in my entity manager like below also didnt work

  @Bean
       public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
           LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
           em.setDataSource(getDataSource());
           //em.setPackagesToScan("com.comp.proj.domain");
           em.setPersistenceUnitName(PERSISTENCE_UNIT_NAME);
           em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
           return em;
       }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!