简体   繁体   中英

Modifying Query Result in JPA

EntityManager em = EMF.get().createEntityManager();
EntityTransaction tx = null;

List<Profile> list = null;
Query q = null;

try{
    tx = em.getTransaction();
    tx.begin();

    q = em.createNamedQuery("Profile.getRandomProfile");
    q.setParameter("random", Math.random());
    q.setMaxResults(8);
    list = (List<Profile>) q.getResultList();

    if (list != null){
        Collections.shuffle(list);
    }

    tx.commit();

} catch(NoResultException ex){
    System.out.println("ERROR CATCHED: " +ex.getMessage());
    if(tx != null && tx.isActive())
        tx.rollback();
} catch(Exception e){
    e.printStackTrace();
}
finally{
    em.close();
}

Shuffling the list have error:

java.lang.UnsupportedOperationException: Query result sets are not modifiable

How to overcome the problem?

Copy the results to a secondary list and shuffle it instead of the query result list.

ArrayList copyList = new ArrayList();
Collections.copy(copyList,list);
Collections.shuffle(copyList);

In the line

list = (List<Profile>) q.getResultList();

after it, you should create a new list based on the result one, like this:

 List<Profile> anotherList= new ArrayList<Profile>(listaOrdenes);

This way you have a "new" list and this one you can modify it.

maybe like this ?

List<Profile> profiles = null;
List<Profile> results = (List<Profile>) q.getResultList();
if(results != null) {
    profiles = new ArrayList<Profile>();
    profiles.addAll(results);
    Collections.shuffle(profiles);
}

Just like the other two people said, you should copy the results to your own list because they come back in read-only mode. The other possibility is that the List implementation that comes back, doesn't support the operations that shuffle invokes. You can also try to see what the type of list is to verify, but I doubt this is the case.

There are 2 options:

1)Create new List (2)Use ORDER BY clause in query.

Collections.sort(...) will sort the list that you give it. So, it will modify the list. However, the list that you are trying to sort is unmodifiable. When Collections.sort(...) calls one of the methods of the list to add or remove an element, it will throw an exception.

One solution is to create a new, modifiable list from the original list, and then sort that list.

// Create a new ArrayList that contains all elements from the list 'identities'
List<Identity> data = new ArrayList<Identity>(identities);

// Sort the new list

Collections.sort(data);

But, since you're presumably getting the list from the database using a JPA query, it would be a better idea to change the database query to include an "order by" clause , to let the database do the sorting. You wouldn't need to do the sorting in your Java code then.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM