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...

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. Commented Feb 21, 2015 at 7: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? Commented Jun 14, 2018 at 13:09

1 Answer 1

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<>();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.