Spring Data Mongo MongoDB DBRef lazy initialization











up vote
4
down vote

favorite












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










share|improve this question
























  • 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 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?
    – pagurix
    Jun 14 at 13:09















up vote
4
down vote

favorite












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










share|improve this question
























  • 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 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?
    – pagurix
    Jun 14 at 13:09













up vote
4
down vote

favorite









up vote
4
down vote

favorite











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










share|improve this question















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







spring spring-data spring-data-mongodb lazy-initialization dbref






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 5:44









Cœur

17k9102140




17k9102140










asked Feb 20 '15 at 11:58









Lombardo

2112




2112












  • 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 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?
    – pagurix
    Jun 14 at 13:09


















  • 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 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?
    – pagurix
    Jun 14 at 13:09
















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 7:59




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 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?
– pagurix
Jun 14 at 13:09




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 at 13:09












1 Answer
1






active

oldest

votes

















up vote
3
down vote













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<>();





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28628447%2fspring-data-mongo-mongodb-dbref-lazy-initialization%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    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<>();





    share|improve this answer

























      up vote
      3
      down vote













      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<>();





      share|improve this answer























        up vote
        3
        down vote










        up vote
        3
        down vote









        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<>();





        share|improve this answer












        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<>();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Aug 1 '15 at 4:12









        Douglas

        1,1531019




        1,1531019






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f28628447%2fspring-data-mongo-mongodb-dbref-lazy-initialization%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Sphinx de Gizeh

            Dijon

            Équipe cycliste