Fórumok

RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

MICHAIL MOUDATSOS, módosítva 11 év-val korábban

RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

Regular Member Bejegyzések: 110 Csatlakozás dátuma: 2011.10.04. Legújabb bejegyzések
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:
userQuery.add(RestrictionsFactoryUtil.in("userId", userExtIdList.toArray()));
userList = (List<user>) UserLocalServiceUtil.dynamicQuery(userQuery);</user>

I got
17:19:11,009 ERROR [BasePersistenceImpl:188] Caught unexpected exception java.lang.ClassCastException
com.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
thumbnail
Mika Koivisto, módosítva 11 év-val korábban

RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

Liferay Legend Bejegyzések: 1519 Csatlakozás dátuma: 2006.08.07. Legújabb bejegyzések
I think you could add new Long[0] into the toArray() so it would look like this:

userQuery.add(RestrictionsFactoryUtil.in("userId", userExtIdList.toArray(new Long[0])));
MICHAIL MOUDATSOS, módosítva 11 év-val korábban

RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

Regular Member Bejegyzések: 110 Csatlakozás dátuma: 2011.10.04. Legújabb bejegyzések
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...
thumbnail
Mika Koivisto, módosítva 11 év-val korábban

RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

Liferay Legend Bejegyzések: 1519 Csatlakozás dátuma: 2006.08.07. Legújabb bejegyzések
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, módosítva 11 év-val korábban

RE: RestrictionsFactoryUtil.in() ClassCastException [!Urgent!]

Regular Member Bejegyzések: 110 Csatlakozás dátuma: 2011.10.04. Legújabb bejegyzések
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:

	Disjunction disjunction = RestrictionsFactoryUtil.disjunction();

	for(MyEntity userExt : userExtList)
	{
		disjunction.add(RestrictionsFactoryUtil.eq("userId", userExt.getUserId()));
	}

	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!