Uploaded image for project: 'JTalks Common'
  1. JTalks Common
  2. JTALKSCOMMON-50

BeanValidation annotations should be placed onto getters, not the fields

VotersWatchers
    XMLWordPrintable

    Details

      Description

      Why the field-based validation is sad:

      • If we place those annotations onto the fields of the class, then Hibernate Validator will access them directly via reflection. This results in the situation when the app won't work under the restrictive SecurityManager.
      • If we need to use interfaces with getters, we can put annotations onto the methods in this interface.
      • If we put annotations on entities managed by Hibernate, and those fields are lazy, the validation will fail since those fields are actually proxies, not our own objects. If we place the annotation upon the getter, then Hibernate will return our own object and the validation won't fail.
      • Sometimes we need to add some logic to the getter which won't be triggered by HV if we use field annotations.
      • Possibility to override validation in subclasses

      Since it doesn't require much of efforts to move those annotations, we need to follow the most safe way: to put those annotations on the public methods. Those annotations should be placed on the getter methods.
      This rule was added to the JTalks Code Conventions.

        Attachments

          Issue Links

            Structure

              Activity

                People

                • Assignee:
                  ancient_mariner Alex Lysak
                  Reporter:
                  ctapobep Stanislav Bashkyrtsev
                • Votes:
                  1 Vote for this issue
                  Watchers:
                  2 Start watching this issue

                  Dates

                  • Created:
                    Updated:

                    Time Tracking

                    Estimated:
                    Original Estimate - 4h
                    4h
                    Remaining:
                    Remaining Estimate - 4h
                    4h
                    Logged:
                    Time Spent - Not Specified
                    Not Specified

                      Structure Helper Panel