Why Ebean returns null for no reason?
Asked Answered
F

1

5

Using Play Framework, I have a model like this :

class MyModel extends Model {
    // Some columns

    @ManyToOne
    public OtherModel other;

    public OtherModel getOther() {
        return other;
    }
}

For a reason I can't understand, if I call myModel.other OR myModel.getOther() (myModel being an instance of MyModel), I got a Null value, even if it should return an instance of OtherModel !

Moreover, if I change the getOther() methods to this :

public OtherModel getOther() {
    console.log (String.valueOf(other));
    return other;
}

getOther() returns the expected instance of OtherModel

Why do I get this, and how to fix this odd behavior?

Forget answered 22/10, 2012 at 12:51 Comment(4)
I had a similar problem (but I did not need the console.log). All I did was just replacing public fields by private ones, and using public getters & setters. I think it was a Playframework bug, but I don't find where I saw that. What version are you using ?Tambac
I'm using 2.0.4 but I encountered the problem since 2.0 soo ... . I'll try what you say, I think I already done that somewhere and it worked so yeah, it seems good. I'll update a comment and if it's this, you can add your comment as an answer so I can accept it.Forget
Well after my initials tests, it seems you are correct! You can answer with your comment, I'll accept it :)Forget
This is probably the biggest "gotcha" when using Play Framework with Ebean. Strange things may happen when ebeans autofetch collides with Play's getter/setter approach. I recommend you reading this great post by Timo to understand what is going on: groups.google.com/forum/#!msg/play-framework/XaGlipnEgBc/…Apatetic
T
5

I had a similar problem (but I did not need the console.log statement).

All I did was just replacing public fields by private ones, and using public getters & setters. I think it was a Playframework bug, but I don't find where I saw that.

Tambac answered 22/10, 2012 at 19:58 Comment(4)
That's odd, I switched all my properties to private, but now, all the ManyToOne returns null (like getOtherModel()). Do you know why ? Is there something I missed?Forget
Ok, after some inspection, it seems to have this problem when I use the model too soon, that's odd.Forget
What do you mean by "using the model too soon" ?Tambac
I use it in a Session.class I made that returns "first needed" objects, like the Account instance of the connected user. And if I'm trying to access his city, (via getCity()), I got null. I did a workaround by doing an home made sql query to get the city. But after that, everything else is working fine. I don't know why.Forget

© 2022 - 2024 — McMap. All rights reserved.