Spring Data MongoDB and JSR Validation using Lazy DBRef

The Problem

Recently using MongoDB and Spring Data I encountered an interesting issue. Saving an Entity  which was loaded by @DBRef(lazy = true) wasn’t possible together with the activated ValidatingMongoEventListener. Each time the following error occurred:

The Issue

Well looking into the details it is quite clear — as an dynamic proxy is passed to the validation which fields are all null. As so any validation on the Entity will always fail. The code of the validator itself down’t really show the obvious miss take — only if you compare it with the save method the problem gets pretty clear:



Compared to the Spring Mongo Mapper code, which is responsible to store the entity it’s clear what we have to do.


As so we have to replace the Spring provided Validation Listener with an own — till Spring fixes the issue:

And of course don’t forget to change the configuration to ensure your fixed class is used.


Pull Request in Spring Data Project

Paul Sterl has written 20 articles

3 thoughts on “Spring Data MongoDB and JSR Validation using Lazy DBRef

  1. Hi there!

    I am having the same issue and followed your same steps. However, both my custom validation listener and Spring’s are being executed.

    I am using Spring 3.2.9 and XML configuration instead, and tried the following configurations:

    Any help fixing this issue would be more than appreciated 😉

      1. Paul Sterl says:

        Yes it needs to be defined in the configuration class / xml. The described behavior points that both validator classes are in the Spring Context active in your case now. (The fixed one and the broken from Spring).

        I would check where the current spring validator is coming from. Either from a imported namespace or some auto magic. Usually (in spring-data) the validator isn’t configured by default. I would assume the same for here now for the XML configuration. –> Means I think you have somewhere in XML the spring validator too. Does this make sense?

        The question is also not about the spring core version but Spring Data / Mongo version. Nevertheless my patch till now didn’t made it into the lib, I can say for sure that spring-data-mongodb is affected until version 1.9.1.RELEASE.

Cancel reply

Leave a Reply to Paul Sterl

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">