8

I'm using Spring + Spring Data MongoDB. My model is like this:

@Document(collection = "actors")
public class Actor extends DomainEntity {

private String name;
private String surname;
@DBRef(lazy = true)
private List<Class> classes;

The other class is pretty generic, so I don't post it. My problem is that the list "classes" isn't loaded when i try to access it, the attribute remains being some kind of proxy object. Example:

Actor a = actorRepository.findOne(id);
//At this moment classes are a proxy object because of the lazy

//Now I try to load the reference and nothing works
a.getClasses();
a.getClasses().size();
a.getClases().get(0).getAttr();
for(Class g:a.getClasses()){
        g.getAttr();
    }

I considered a ton of options, but no way to make it working...

Cœur
  • 37,241
  • 25
  • 195
  • 267
Lombardo
  • 81
  • 1
  • 1
  • 2
  • So, what exactly is the problem? What doesn't work? Which exceptions do you get? What would you like to see? If you don't want a proxy for `Class`, don't make it lazy. – Oliver Drotbohm Feb 21 '15 at 07:59
  • I think he wants to use lazy, but that in a particular case he needs the bean read from the DB to be fully exploited. This is possible? – pagurix Jun 14 '18 at 13:09

1 Answers1

7

I'm using spring-data-mongodb-1.7.0.RELEASE and I was able to solve this issue by initializing the lazy-loaded collection in its declaration, for instance:

@DBRef(lazy = true)
private List<Class> classes = new ArrayList<>();
Douglas
  • 1,313
  • 1
  • 14
  • 19