Categories: java, sql, json, spring, hibernate

Hibernate: Criteria & createSQLQuery: how to get proper JSON?

1 answer

When I try this I get the proper JSON as a result, but it takes a lot of time:

        Criteria c = sessionFactory.getCurrentSession().createCriteria(User.class);         List<User> users = c.list();         List<User> specialUsers = new ArrayList<>();          for (User user : users) {             List<Perm> userPerms = user.getProfile().getPerms();              for (Perm perm : userPerms) {                 if (perm.getId().equals(SPECIAL_ID)) {                     specialUsers.add(user);                 }             }         }          return specialUsers; 

and the JSON is like:


In attempt to improve performance I tried code below. In SQL app the results are OK, a few records of users:

        String sql = "SELECT, FROM app.user u inner join app.perms p where u.profile = p.profile AND p.right= :rightId";         List<User> specialUsers= (List<User>)sessionFactory.getCurrentSession()                 .createSQLQuery(sql)                 .setParameter("rightId", SPECIAL_ID)                 .list();          return specialUsers; 

Now the 'JSON' however looks like this:


I tried several things, like select *, criteria.add(Restrictions...) but to no effect. What I noticed is that in the first case specialUsers.toString returns proper data, in the second case it returns meaningless Strings like Ljava.lang.Object;@23e1469f.

Any hints how to solve this?

All answers to this question, which has the identifier 61234328

The best answer:

I managed to solve this in this way, may not be perfect:

        // get ids of all special users         String sql = "SELECT FROM app.user u inner join app.perms p where u.profile = p.profile AND p.right= :rightId";         List<Integer> intIds = sessionFactory.getCurrentSession()                 .createSQLQuery(sql)                 .setParameter("rightId", SPECIAL_ID)                 .list();          // convert to long values         List<Long> longIds =                 .mapToLong(Integer::longValue)                 .boxed().collect(Collectors.toList());          // get all special users         Criteria c = sessionFactory                 .getCurrentSession()                 .createCriteria(User.class)                 .add("id", longIds));          List<User> specialUsers = c.list();         return specialUsers;     }      

Last questions

how do i remove the switch on my home screen?
how to edit the JS date and time to update atuomatically?
How to utilize data stored in a multidimensional array
Powermockito not mocking URL constructor in URI.toURL() method
Android Bluetooth LE Scanner only scans when phone's Location is turned on in some devices
docker wordpress container can't connect to mysql container
How can I declare a number in java that is more than 64-bits? [duplicate]
Optaplanner solutionClass entityCollectionProperty should never return null error when simple JSON object passed to controller
Anylogic, get the time a pedestrain is in a queue
How do I fix this syntax issue with my .flex file?
Optimizing query in PHP
How to find the highest number of a column and print two columns of that row in R?
Ideas on “Error: Type is referenced as an interface from”?
JCIFS SmbFile.exists() and SmbFile.isDirectory() return false when it exists and I can listFiles()
PHP total order
Laravel booking system design
neural net - undefined column selected
How to indicate y axis does not start from 0 in ggplot?
Fragments in backStack
Spinner how to change the data