-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: NamedQuery limitations with parameters
PostPosted: Wed Oct 07, 2009 10:30 am 
Newbie

Joined: Wed Oct 07, 2009 9:40 am
Posts: 4
Product Version: NetBeans IDE 6.5 (Build 200811100001)
Java: 1.6.0_10; Java HotSpot(TM) Client VM 11.0-b15
System: Windows XP version 5.1 running on x86; Cp1252; nl_NL (nb)
Framework: JavaServer faces


Due to limitation of the usage of parameters in named queries I have been forced to do operations on the output list that might have been possible in a named query.
The approach I have taken works, but is too slow when the number of records increases (200 is fine, 2000 not anymore).
I hope anyone can give me some advice, I did highlight my specific questions.

I have to do four steps:

    1. Get a list with a specific feature
    2. Apply user specified filters (if any) on that list
    3. Hide (part of) records that specific users are not allowed to see
    4. Sort the result via one of 4 options the user can pick

I can only do the first step via a named query.

The reason the filter does not work is that the filter is applied on a linked table twice. Example (person is nicely linked):

Code:
Select Rol r Where
r.person.name = :name AND
r.person.city = :city

This runs into a nullpointer, which is explained here: http://opensource.atlassian.com/project ... e/HHH-2159

QUESTION: The patch on the above page, can I apply that myself? If so, how?

Then I tried to do the above in two steps:

Code:
Step 1: Select Person p WHERE name=:name and city=:city
Step 2: Select Rol r WHERE person in (:person)

The second step gives a compilation error. I cannot use parameters like this.

QUESTION: Is there another syntax for what I am trying to do in step 2?

I now do step 2 by running though the List<Rol> and using string compares (startWith actually). This is of course, slow, and profit any index in the database.

Step 3 does not give me any problems. If I could do step 2 with a NamedQuery, I could probably put step 3 in this as well. Step 3 has to be done before step 4, because, if a user has no rights to see an address, sorting on zipcode might give away something. See this small example:

    10292
    <secured data>
    10294

For sorting I tried to specify the sorting column(s) via a parameter. (Select Rol r Order by :order). This also was not an acceptable place for a parameter. Since the number of sorting methods is limited (4) I created 4 named queries and called the one I needed. This worked fine, but that was before I was putting in the filter.

Now I use Collections.sort with a sorting function as parameter. I think that works just as fine as sorting in the Namedquery. But I need to test that more.

One of the reasons I would like to use the NamedQuery variant, is that I could make use of the sublist function. That function should also speed up larger lists, by displaying them one page at a time. However, I do want to have the sorting over the whole list, not only over the displayed items.

QUESTION: Can I use parametered search in a NamedQuery?

Finally, if the plan to do this via NamedQueries is not going to work. Are there other tips to filter a List?

Thanks if you have read this far!


Top
 Profile  
 
 Post subject: Re: NamedQuery limitations with parameters
PostPosted: Wed Oct 07, 2009 12:13 pm 
Newbie

Joined: Mon May 04, 2009 6:33 pm
Posts: 3
For the second part, have you tried

Code:
Query query = session.createQuery("from person in (:person)");
query.setParameterList("person", persons);
List<Person> persons = query.list();


Top
 Profile  
 
 Post subject: Re: NamedQuery limitations with parameters
PostPosted: Thu Oct 08, 2009 4:48 am 
Newbie

Joined: Wed Oct 07, 2009 9:40 am
Posts: 4
Thanks for the suggestion.

I tried that in various ways, but I get a runtime error "unexpected token".
The function setParameterList I do not have. Maybe we have different versions of hibernate/EJB?

I have something like this (simplyfied):

In PersonFacade:
Code:
public List<Person> findByName(String name) {
        Query query = em.createNamedQuery("Person.findByName");
        query.setParameter("name", name);
        return query.getResultList();
}

In ContactFacade:
Code:
        List<Person> personList = personFacadeLocal.findByName(name);
        // Trouble in next line
        Query query = em.createQuery("SELECT c FROM Person in (:person), Contact c);
        query.setParameter("person", personList);
        List<Contact> contactList = query.getResultList();

The second line is giving me the trouble here. What could be the exact formatting of that line? And will the EJB link between Person and Contact still work?


Top
 Profile  
 
 Post subject: Re: NamedQuery limitations with parameters
PostPosted: Thu Oct 08, 2009 5:25 am 
Newbie

Joined: Wed Oct 07, 2009 9:40 am
Posts: 4
Ah, wait, I see you use org.hibernate.query where I was using javax.persistence.Query. Will check out the other option.


Top
 Profile  
 
 Post subject: Re: NamedQuery limitations with parameters
PostPosted: Thu Nov 12, 2009 6:56 am 
Newbie

Joined: Wed Oct 07, 2009 9:40 am
Posts: 4
Found it!

The correct syntax is:

Code:
Select Rol r where r.Person in
(select p from Person p where name=:name and city=:city)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.