GithubHelp home page GithubHelp logo

jsr303-hibernate-events-injection's Introduction

This small project demonstrates how to configure Spring's bean injection with Hibernate event-based validation.

The reference Hibernate Validator documentation states that it is possible to configure Hibernate to validate beans before three events:

  • Before saving (pre-insert)
  • Before updating (pre-update)
  • Before deleting (pre-update)

Although this is convenient for simple validators, more complex custom validators might require that you inject references to beans managed by your Spring context. The example presented in Hibernate Validator's documentation doesn't mention this possibility. Although it is possible to do it, the exact way may not be immediately obvious.

This project defines:

  • A custom validator (com.github.davidmarquis.model.validation.CustomValidationValidator) that has an @Autowired bean injected.
  • A User model (com.github.davidmarquis.model.User) that uses this custom validation rule on the email field.
  • A JUnit test case that simply tries to save a User instance on a new Hibernate session. This triggers Hibernate's pre-insert event which, in turn, instantiates and triggers our custom validator, with autowired dependencies injected. Great success!
  • Everything is wrapped up together in src/main/resources/applicationContext.xml.

Run maven test in the project root to see this in action.

jsr303-hibernate-events-injection's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

bbruneel shekii

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.