I am working on a project where using JPA and the following code I am able to retrieve from DB a list of objects which consist the profile of a user. Each object is a two field object (word, frequency)
public class JpaUserprofilesDao implements UserProfilesDao {
@PersistenceUnit
private EntityManagerFactory emf;
EntityManager em;
public EntityManager getEntityManager() {
return emf.createEntityManager();
}
@Transactional
@Override
public List<Object[]> getUserProfiles(Long userId){
em = getEntityManager();
try {
List<Object[]> up =em.createQuery("SELECT up.userprofilesPK.word, up.userprofilesPK.frequency from Userprofiles up WHERE up.userprofilesPK.userid= :userid").setParameter("userid", userId).getResultList();
return up;
} finally {
em.close();
}
}
}
In another class of my project I am using the following block of code in order to put the above information in a Map and be able to manipulate it.
profileObject = peristanceservice.getUserProfiles(userId);
for(Object[] s: profileObject ){
if(!tempList.contains(s[0].toString())){
bag.put( (String) s[0].toString(), Integer.parseInt(s[1].toString()) );
tempList.add(s[0].toString());
}
}
Even though I am getting the result it takes so long time to process the list of objects returned by the persistance service and putting them to the Map, that finally prove useless. Is there a way to get the Map directly from the persistance service?
Thank you in advance.