Tribune

Home » Liferay Portal » English » 3. Development

Vista Combinata Vista Piatta Vista ad Albero
Discussioni [ Precedente | Successivo ]
toggle
MICHAIL MOUDATSOS
RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]
23 maggio 2012 10.43
Risposta

MICHAIL MOUDATSOS

Punteggio: Regular Member

Messaggi: 110

Data di Iscrizione: 4 ottobre 2011

Messaggi recenti

Hello all

I ll get right to the issue: I have used DynamicQuery along with RestrictionsFactoryUtil.in(String, Collection) in the past and it worked just fine. The DynamicQuery concerned a service class of mine.
I recently tried to use the DynamicQuery API for liferay's User class. When I tried:
1userQuery.add(RestrictionsFactoryUtil.in("userId", userExtIdList.toArray()));
2userList = (List<User>) UserLocalServiceUtil.dynamicQuery(userQuery);

I got
117:19:11,009 ERROR [BasePersistenceImpl:188] Caught unexpected exception java.lang.ClassCastException
2com.liferay.portal.kernel.exception.SystemException: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long


from my portlet code I set up the DynamicQueryFactoryUtil with the classloader and all and I also tried from a XXXLocalServiceImpl without it.
I tried to call RestrictionsFactoryUtil.in() with both List and Object[] params.

Does anyone have a clue why this happens? Note that the List I pass as argument to .in() actually contains Long objects (it is the result of another DynamicQuery and I have printed out the simple name of the class of its contents to be sure. As you see it also fails when I'm calling .toArray(), in case that the fact that the List I used was immutable, had anything to do with my problem... (Unless the call toArray() propagates the -hypothetical- problem)

From the error message it looks like that for some reason, my Long objects or their values are somehow transformed to integer values and when the .in() part of the query is about to run it produces error since it expects longs.... I use SQL server and the userId is of type BIGINT

Thank you in advance
Mika Koivisto
RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]
23 maggio 2012 15.05
Risposta

Mika Koivisto

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1498

Data di Iscrizione: 7 agosto 2006

Messaggi recenti

I think you could add new Long[0] into the toArray() so it would look like this:

1userQuery.add(RestrictionsFactoryUtil.in("userId", userExtIdList.toArray(new Long[0])));
MICHAIL MOUDATSOS
RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]
23 maggio 2012 22.40
Risposta

MICHAIL MOUDATSOS

Punteggio: Regular Member

Messaggi: 110

Data di Iscrizione: 4 ottobre 2011

Messaggi recenti

Unfortunately not. I tried with userExtIdList.toArray(new Long[userExtIdList.size()])), because I think that's the proper way to use that method, to no avail. I didn't expect it to work to be honest. I had tried so many similar things, so I was almost sure. Can you think of anything else? I ll repeat that I have successfully tried this in the past with a List<Integer> and it worked as expected. I didn't want to make a bulk query and start iterating through results so I used DynamicQuery for this purpose. With these errors I'm afraid I'll be forced to do that iteration after all...
Mika Koivisto
RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]
24 maggio 2012 13.58
Risposta

Mika Koivisto

LIFERAY STAFF

Punteggio: Liferay Legend

Messaggi: 1498

Data di Iscrizione: 7 agosto 2006

Messaggi recenti

Unfortunately no. I haven't really used the DynamicQuery API all that much. I checked JIRA to see if there's any know issues but didn't find anything that I though was relevant. You might want to double check if you are using something older that 6.1.
MICHAIL MOUDATSOS
RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]
25 maggio 2012 6.45
Risposta

MICHAIL MOUDATSOS

Punteggio: Regular Member

Messaggi: 110

Data di Iscrizione: 4 ottobre 2011

Messaggi recenti

Mika Koivisto:
You might want to double check if you are using something older that 6.1.
I forgot to tell that I'm using Liferay 6.0.6, my mistake - sorry

UPDATE: The closest thing I could do to avoid iterating through a bulk query was to add multiple disjoint criteria on the same property:
 1
 2    Disjunction disjunction = RestrictionsFactoryUtil.disjunction();
 3
 4    for(MyEntity userExt : userExtList)
 5    {
 6        disjunction.add(RestrictionsFactoryUtil.eq("userId", userExt.getUserId()));
 7    }
 8
 9    userQuery.add(disjunction);
It might look ugly but it lets you narrow your results and hence your appserver-dbserver traffic. I don't know how DynamicQuery exactly implements this approach but I assume it is essentially the same as doing the .in(List), if both approaches are finally translated to a single SQL query

In the meanwhile if anyone has any clue to why RestrictionsFactoryUtil.in() does not work when it is referring to a primary key of type long, please let me know!