What causes this exception?

java.lang.UnsupportedOperationException: This operation is not supported on Query Results at org.datanucleus.store.query.AbstractQueryResult.contains(AbstractQueryResult.java:250) at java.util.AbstractCollection.retainAll(AbstractCollection.java:369) at namespace.MyServlet.doGet(MyServlet.java:101) 

I am trying to take one list that I extracted from a data warehouse query and save only those results that are also in the list that I got from the list of keys. Both of my lists are populated as expected, but I apparently can't save UserAll on any of them.

 // List<Data> listOne = new ArrayList(query.execute(theQuery)); // DatastoreService ds = DatastoreServiceFactory.getDatastoreService(); // List<Data> listTwo = new ArrayList(ds.get(keys).values()); // listOne.retainAll(listTwo); 

EDIT

Well, in an attempt to simplify, as this seems to have several problems in one, I stopped using the low-level API for data storage and instead of just pulling one after the other with a loop.

  List<MyClass> test = (List<MyClass>) query.execute(); List<MyClass> test2 = new ArrayList<MyClass>(); for (String key : favorites) { test2.add(pm.getObjectById(MyClass.class, key)); } log.info(test.toString()); test.retainAll(test2); 

The above work. This is no exception. The following is an exception. The only difference is log.info. I'm at a dead end.

  List<MyClass> test = (List<MyClass>) query.execute(); List<MyClass> test2 = new ArrayList<MyClass>(); for (String key : favorites) { test2.add(pm.getObjectById(MyClass.class, key)); } test.retainAll(test2); 
+4
source share
3 answers

It will not allow me to make a new ArrayList () as a result of the request, since it returns an array of objects.

However, you need to put them in new ArrayList() . The returned List implementation does not seem to support retainAll() . This is what the exception tells you.

A "plain" ArrayList supports it. If passing through the ArrayList constructor is not possible due to differences in the generic type, you will need to manually iterate over it and drop each element before adding it.

 List<Data> listTwo = new ArrayList<Data>(); for (Object object : ds.get(keys).values()) { listTwo.add((Data) object); } listOne.retainAll(listTwo); 

Update : according to your update, entities are obviously lazy loading / filling. Most ORMs (DataNucleus - this one) can actually do this. Since I do not use DataNucleus, I can not talk in detail about how to fix this in a "beautiful" way. But at least now you know the root cause of the problem, and you can solve it the same way as described above. Fill the test list in a loop.

+2
source

If the type of collection that you use for your "key list" does not support retainAll , this exception will be thrown. What type are you using?

+1
source

TIP: you do not need to iterate to populate listTwo. just do:

listTwo.addAll(ds.get(keys).values())

0
source

Source: https://habr.com/ru/post/1310312/


All Articles