0

I am trying get a list with Hibernate. I get a JSONArray with the IN values. JSONArray rolesEnSession is, for example, ["ROLE1", "ROLE2"]

public List<MenuLeft> getMenu(JSONArray rolesEnSession){


        String listaRoles = "";

        for (int i = 0; i < rolesEnSession.length(); i++) {
            try {
                listaRoles =  listaRoles + "'"+rolesEnSession.getString(i)+",";
            } catch (JSONException ex) {
                Logger.getLogger(LinkDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return getCurrentSession()
                .createQuery("from MenuLeft "
                        + "WHERE menuRol in (:rolesEnSession)")
                .setParameter("rolesEnSession", listaRoles)
                .list();
    }

But I get a blank value. The column menuRol in database has values ROLE1 and ROLE2

1 Answers1

0

Consider listaRoles after two iterations with your current code. It will have value:

'ROLE_1,'ROLE_2,

Instead of trying to build a well-formed string of parameter values, just change listaRoles to be an actual List and pass that in as a parameter. Let Hibernate handle the conversion to a valid SQL.

darrengorman
  • 12,952
  • 2
  • 23
  • 24