0

I'm using the following criteria to retrieve all Person objects that have certain roles granted (in the PersonService):

@Transactional(readOnly = true)
List findAllByRoles(authorities) {
    Person.createCriteria().list {
            personRoles {
                role {
                    'in'('authority', authorities)
                }
            }
            order('name', 'asc')
        }.unique(false)
}

I now have the problem that it returns a List with Person__$$__javassist_67 objects rather than Person objects.

How could I alter the statement in order to retrieve Person objects?

EDIT:

I need this because I'm using the list I get here in connection with another list of Person objects. As I wanted to use removeAll on one of the two lists both need to contain objects of the same type what was not the case.

gabriel
  • 347
  • 3
  • 18
  • The better question is why are the proxy objects causing a problem for you? – James Kleeh Dec 03 '15 at 14:41
  • Normally they wouldn't but I'm using the list I get here in connection with another list of `Person` objects. As I wanted to use `removeAll` on one of the two lists both need to contain objects of the same type what was not the case. – gabriel Dec 03 '15 at 15:29
  • So the issue is the `equals` method. You can implement that method to check against any unique identifier and that process will work, proxy or not – James Kleeh Dec 03 '15 at 18:39
  • Thanks, that helped a lot. Enter it as an answer and you'll get the credit - I have updated my question accordingly. – gabriel Dec 04 '15 at 08:19

2 Answers2

1

Implement the equals method on Person to be able to identify if 2 instances are equal, which will work across proxy

James Kleeh
  • 12,094
  • 5
  • 34
  • 61
0

The Person__$$__javassist_67 object is just a proxy for the Person class in this instance, caused most likely by lazy fetching. I'm not sure why you are getting it in this situation. I would attempt to explicitly set the fetchMode:

import org.hibernate.FetchMode
...
Person.createCriteria().list {
    fetchMode("personRoles", FetchMode.JOIN)
    fetchMode("role", FetchMode.JOIN)
    personRoles {
        role {
            'in'('authority', authorities)
        }
    }
    order('name', 'asc')
}.unique(false)

You may not need the second fetchMode.

tylerwal
  • 1,858
  • 2
  • 15
  • 20