"Cannot resolve query" - how to fix for named queries?

Intellij 9 UE does not find my named queries in a JPA context.

Query q = em.createNamedQuery("selectAclEntriesByUser");

will be marked as incorrect with "cannot resolve query". The named query exists and is declared via annotations in the same file.
Is this the expected behaviour or is there a way to get the IDE to resolve named queries?

7 comments

Hello Ingo,

Please post here a bit more about your setup.
Module dependencies, location of persistence unit, JPA facets configuration, etc.

Regards,
Gregory Shrago


On 05.05.2010 13:31, Ingo Wiarda wrote:

Intellij 9 UE does not find my named queries in a JPA context.

>

Query q = em.createNamedQuery("selectAclEntriesByUser");

>

will be marked as incorrect with "cannot resolve query". The named query exists and is declared via annotations in the same file.
Is this the expected behaviour or is there a way to get the IDE to resolve named queries?

>

---
Original message URL: http://www.jetbrains.net/devnet/message/5262520#5262520


0

Hello Gregory, thanks for your reply.

While preparing the following reply, I found that the original error message has vanished and now another warning is present: query.setParameter("foo",foo) will now show a "cannot resolve query parameter" warning.

1. Location of persistence unit:
The persistence unit is located in another module in the same project. It is referenced by Module Settings => JPA => JPA Configuration Descriptor.

<persistence-unit name="cinnamon_test">
     
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     
     <class>server.data.ObjectSystemData</class>
     <class>server.Acl</class>
     <!-- ... more classes ... -->

      <properties>
       <property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
           <property name="hibernate.archive.autodetection" value="class,hbm"/>     
        <property name="hibernate.hbm2ddl.auto" value="create"/>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.release_mode" value="after_transaction"/>
        <property name="hibernate.c3p0.min_size" value="10" />
        <property name="hibernate.c3p0.max_size" value="150"/>
        <property name="hibernate.c3p0.timeout" value="300"/>
        <property name="hibernate.c3p0.max_statements" value="200"/>
        <property name="hibernate.c3p0.idle_test_period" value="3000"/>
        <property name="current_session_context_class" value="thread" />
      </properties>
      
</persistence-unit>




2. Module dependencies

  • Hibernate Annotations 3.4.0
  • Hibernate EntityManager 3.4.0
  • Hibernate Core 3.3.1
  • others - I have added the javaee-jar as required by the JPA-facet, even though this is not strictly a J2EE-application.


3. Abbreviated example of problematic class

package server;

import org.dom4j.Element;
import server.global.Constants;
import server.i18n.LocalMessage;

import javax.persistence.*;
import java.io.Serializable;
import java.util.*;

@NamedQueries(
     {          
     // AclEntries
     @NamedQuery(
          name = "selectAllAclEntries",
          query = "select ae from AclEntry ae"
     ),
     @NamedQuery(
          name = "selectAclEntriesByUser",
          query = "select ae from AclEntry ae where ae.acl=:acl and ae.group in (select g.group from GroupUser g where g.user=:user)"
     ),
     }
)
@Entity
@Table(name = "acls",
          uniqueConstraints = {@UniqueConstraint(columnNames={"name"})}
)
public class Acl implements Serializable{
     
     private static final long     serialVersionUID     = 1L;

     @Id @GeneratedValue
     @Column(name = "id")
     private long id = 0;

     @Column(name = "name",
               length = Constants.NAME_LENGTH,
               nullable = false)
     private String name;
     
     @Column(name = "description",
               length = Constants.DESCRIPTION_SIZE,
               nullable = false)
     private String description;

     @Version
     @Column(name="obj_version")
     @SuppressWarnings("unused")
     private Long obj_version = 0L;
     
     @OneToMany(mappedBy = "acl",
               cascade = { CascadeType.PERSIST, CascadeType.REMOVE }
     )
     private Set<AclEntry> aclEntries = new HashSet<AclEntry>();
     
     @OneToMany(mappedBy = "acl",
               cascade = { CascadeType.PERSIST, CascadeType.REMOVE }
     )
     private Set<CustomTable> customTables= new HashSet<CustomTable>();
     
     private transient Map<User, List<AclEntry>> userEntries = new HashMap<User, List<AclEntry>>();
     
     public Acl (){     

     }

     public Acl(String name, String descripton){
          this.name = name;
          this.description = descripton;
     }
     
     // getters & setters

     
     /**
      * Cache the results of a query for the aclEntries a user has for this Acl.
      * @param user the user whose AclEntries are being queried.
      * @param em current EntityManager
      * @return a List of AclEntries to which the user belongs.
      */
     @SuppressWarnings("unchecked")
     public List<AclEntry> getUserEntries(User user, EntityManager em){
          if(userEntries.containsKey(user)){
               return userEntries.get(user);
          }
          else{
               Query q = em.createNamedQuery("selectAclEntriesByUser");
               q.setParameter("acl", this);
               q.setParameter("user", user);
               userEntries.put(user, (List<AclEntry>) q.getResultList());
               return userEntries.get(user);
          }     
     }
}


Original source code: http://cinnamon.svn.sourceforge.net/viewvc/cinnamon/EntityLib/trunk/src/server/Acl.java?revision=1887&view=markup

0

The query is resolved then, you can verify this by navigating the  reference.
As far as "cannot resolve query parameter" warning is concerned,
please check that the query indeed has parameter named "foo" (there is none in the supplied code).

You may also try completion inside quotes to get the list of available parameters.

On 05.05.2010 18:41, Ingo Wiarda wrote:

Hello Gregory, thanks for your reply.

>

While preparing the following reply, I found that the original error message has vanished and now another warning is present:
query.setParameter("foo",foo) will now show a "cannot resolve query parameter" warning.

0

Der Gast schrieb::

As far as "cannot resolve query parameter" warning is concerned,
please check that the query indeed has parameter named "foo" (there is none in the supplied code).

You may also try completion inside quotes to get the list of available parameters.

"foo" was only used as an example - in fact, every case of query.setParameter(paramName) is marked with "cannot resolve query parameter". The queries are working, the parameter names are correct.

Completion inside quotes does not give a list of available parameters.

This is not a critical problem, but as IntelliJ IDEA seems to be able to analyze query.createNamedQuery(...)*, it would be nice if it could also parse query.setParameter(...) correctly.



* the ability to jump to a named query's declaration is quite useful.

0

Hi Ingo,

It works for me. As Gregory mentions, a very minimal project to reproduce will likely help to track down the problem.

-tt

0

Hi,
I have put the lib into its own project.
I hope you are able to reproduce the problem, as it seems somewhat erratic - the error messages seem to change with the tide... (the example project shows the "cannot resolve query parameter" problem, but the original project now reports that numerous class files inside the same package cannot be found).
The iml-files seem to be mostly identical.

Thanks for taking the time to answer my questions

Ingo



Attachment(s):
EntityLib001.zip
0

IntelliJ adding the JPA and it's doing the validation.You can remove that JPA so that these validation errors will not appear.
Go to settings --> modules-->Expand your project and see if any JPA added. If added select that JPA and delete.
This worked for me.

0

Please sign in to leave a comment.