BlackLabs / play-morphia

Provide mongodb access service to play.db.Model via Morphia
Apache License 2.0
130 stars 51 forks source link

NullPointerException when creating a Model which has an @Embeded entity that doesn't exists. #112

Closed ivansjg closed 10 years ago

ivansjg commented 10 years ago

First of all I want to say that I don't know if this patch should be included in the official repo or not, since I made it for my needs (but I think these needs are very close to the logical ones, and this is why I submitted the pull request to the official repo).

Also I want to advice that I know that an @Embedded model must not have an @Entity annotation, but I have to do that because I want to have a collection only for this model, and also I want to have it embedded in another Model.

So, the situation is the following:

Model User (has @Entity) @Embedded public Language preferredLanguage;

Model Language (has @Entity)

Controller: public static class register(@Valid User user)

View: The view has a form which sends the field "user.language._id" with null or with an ID which doesn't belong to any existant language.

In this scenario, Play!, when takes care about the validation, tries to create a new model User, and play-morphia plugin tries to embed to that User an embedded language which doesn't exists. So, a NullPointerException will be thrown in play-morphia plugin.

Well, once I have explained the scenario, I think the next will be more understandable. The NullPointerException is thrown because the q.get() that is in Model.java returns null, not NotResultException. I don't know if I'm correct but I didn't see anywhere throwing a NotResultException, so I thought that the correct way to handle this scenario is checking if the model exists or not (if q.get() returns null or not).

As I already said, I don't know if I'm correct or not, but I fixed it and it solves my problem. So I will make a pull request to fix this ASAP, and later you decide what to do.