{"id":1477,"date":"2015-02-27T23:05:27","date_gmt":"2015-02-27T22:05:27","guid":{"rendered":"http:\/\/codingexplained.com\/?p=1477"},"modified":"2017-06-11T19:18:17","modified_gmt":"2017-06-11T17:18:17","slug":"unique-field-validation-using-hibernate-spring","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring","title":{"rendered":"Unique Field Validation Using Hibernate and Spring"},"content":{"rendered":"<p>There are quite a few approaches to validating whether or not a given value already exists in a data store or not. In this article, I will show my solution to this problem by using Hibernate Validator in combination with Spring Framework.\u00a0As you will see, the latter is solely used for injecting a dependency into my custom validator, so using the validator without Spring Framework should not be too much work.<\/p>\n<h2>Other Approaches<\/h2>\n<p>Most other solutions that I have found\u00a0use one of the following approaches to implement a unique constraint validator:<\/p>\n<ul>\n<li>Inject an <a href=\"http:\/\/stackoverflow.com\/questions\/4613055\/hibernate-unique-key-validation#answer-4733441\" target=\"_blank\" rel=\"external nofollow\">EntityManager into the custom validator<\/a>, or alternatively <a href=\"https:\/\/developer.jboss.org\/wiki\/AccessingTheHibernateSessionWithinAConstraintValidator\" target=\"_blank\" rel=\"external nofollow\">a SessionFactory<\/a><\/li>\n<li>Inject a repository\/DAO into the custom validator<\/li>\n<li>Inject a specific service into the custom validator<\/li>\n<li>A solution that does not use a custom validator and thereby does not conform with <a title=\"JSR 303 specification\" href=\"http:\/\/beanvalidation.org\/1.0\/spec\/\" target=\"_blank\" rel=\"external nofollow\">JSR 303<\/a><\/li>\n<\/ul>\n<p>Personally, I do not fancy using an <span class=\"code\">EntityManager<\/span> or <span class=\"code\">SessionFactory<\/span> directly within a validator, because it violates the architecture of my application (MVC + service layer + repository layer); I want all of my business logic to reside within services, hence why checking whether or not a given value exists in my data store should be done within a service. The same applies for using a repository or DAO (Data Access Object) directly within a validator, as this approach also bypasses the service layer. This may not be a problem for you depending on your application&#8217;s architecture, but unless you are dealing with a legacy system that is difficult or time consuming to change the architecture of, then you should strongly consider implementing a service layer.<\/p>\n<p>It is also common to inject a specific service instance into a custom validator. This is actually a good solution, but in my case I wanted a solution that was more generic such that I would not have to implement a new validator for each and every time I wanted to validate the uniqueness of a value. Solutions that do not use bean validation (<span class=\"code\">JSR 303<\/span>) did also not seem feasible because I want to be able to validate a bean with the <span class=\"code\">@Valid<\/span> annotation before a parameter in a Spring MVC controller action.<\/p>\n<h2>My Solution<\/h2>\n<p>To overcome these shortcomings, I set out to implement a different approach. Truth be told, my approach has a few shortcomings as well, but I will get back to that. First,\u00a0let me briefly explain how my solution works.<\/p>\n<p>The core concept of my solution is to inject a service into a custom validator. Where it differs from other solutions that I have seen is that the injected service is dynamic such that the validator can be reused in different contexts (for different fields). This is done by requiring the service to be of type <span class=\"code\">FieldValueExists<\/span>, which is just a custom interface that I created. You could name this interface anything you want.<\/p>\n<pre><code class=\"java\">public interface FieldValueExists {\r\n    \/**\r\n     * Checks whether or not a given value exists for a given field\r\n     * \r\n     * @param value The value to check for\r\n     * @param fieldName The name of the field for which to check if the value exists\r\n     * @return True if the value exists for the field; false otherwise\r\n     * @throws UnsupportedOperationException\r\n     *\/\r\n    boolean fieldValueExists(Object value, String fieldName) throws UnsupportedOperationException;\r\n}<\/code><\/pre>\n<p>I will explain the semantics of this interface later, after seeing how it is used within the custom Hibernate validator. But first we need an annotation that we can use within classes to apply the unique constraint.<\/p>\n<pre><code class=\"java\">@Target({ METHOD, FIELD, ANNOTATION_TYPE })\r\n@Retention(RUNTIME)\r\n@Constraint(validatedBy = UniqueValidator.class)\r\n@Documented\r\npublic @interface Unique {\r\n    String message() default \"{unique.value.violation}\";\r\n    Class&lt;?&gt;[] groups() default {};\r\n    Class&lt;? extends Payload&gt;[] payload() default {};\r\n    Class&lt;? extends FieldValueExists&gt; service();\r\n    String serviceQualifier() default \"\";\r\n    String fieldName();\r\n}<\/code><\/pre>\n<p>If you are not familiar with the above, then I suggest that you read <a title=\"Creating custom Hibernate constraints\" href=\"https:\/\/docs.jboss.org\/hibernate\/validator\/4.0.1\/reference\/en\/html\/validator-customconstraints.html#validator-customconstraints-constraintannotation\" target=\"_blank\" rel=\"external nofollow\">this part of the Hibernate documentation<\/a>. The target can be either a method, a field or an annotation, where the latter allows this annotation to be grouped with other constraints in a new annotation. With the <span class=\"code\">@Constraint<\/span> annotation, we define which class that implements the constraint &#8211; in this case I decided to name the class <span class=\"code\">UniqueValidator<\/span>. We define a required <span class=\"code\">service<\/span> attribute, which is any class that implements our <span class=\"code\">FieldValueExists<\/span> interface. Furthermore, we require a <span class=\"code\">fieldName<\/span> attribute to be passed, which is simply the name of the field for which we want to check if a given value exists. I will explain why this attribute is needed in a moment, as well as the <span class=\"code\">serviceQualifier<\/span> attribute. Please note that the <span class=\"code\">message<\/span> attribute (which is the error message if the constraint&#8217;s <span class=\"code\">isValid<\/span> method returns <span class=\"code\">false<\/span>) is a message key, because we have a string enclosed within curly brackets. Therefore you must either define this key within your message source (<a href=\"https:\/\/docs.jboss.org\/hibernate\/validator\/5.1\/reference\/en-US\/html\/chapter-message-interpolation.html#section-message-interpolation\" target=\"_blank\" rel=\"external nofollow\">see here for details<\/a>), or remove the brackets and provide an error message directly within the annotation. The latter of course means that you then cannot translate the message.<\/p>\n<p>Now, let us have a look at the custom constraint implementation.<\/p>\n<pre><code class=\"java\">public class UniqueValidator implements ConstraintValidator&lt;Unique, Object&gt; {\r\n    @Autowired\r\n    private ApplicationContext applicationContext;\r\n\r\n    private FieldValueExists service;\r\n    private String fieldName;\r\n\r\n    @Override\r\n    public void initialize(Unique unique) {\r\n        Class&lt;? extends FieldValueExists&gt; clazz = unique.service();\r\n        this.fieldName = unique.fieldName();\r\n        String serviceQualifier = unique.serviceQualifier();\r\n\r\n        if (!serviceQualifier.equals(\"\")) {\r\n            this.service = this.applicationContext.getBean(serviceQualifier, clazz);\r\n        } else {\r\n            this.service = this.applicationContext.getBean(clazz);\r\n        }\r\n    }\r\n\r\n    @Override\r\n    public boolean isValid(Object o, ConstraintValidatorContext constraintValidatorContext) {\r\n        return !this.service.fieldValueExists(o, this.fieldName);\r\n    }\r\n}<\/code><\/pre>\n<p>So the validator has three fields; the application context, the service that is to be used for the validation, and the name of the field. First the <span class=\"code\">initialize<\/span> method is invoked with the annotation as an argument, from which we can get its attribute values. First we get the <span class=\"code\">service<\/span> argument, which is the type of the class that we will use for the validation, such as <span class=\"code\">UserService<\/span>. This class is used to fetch a bean of that type from the application context that we have let Spring Framework autowire into our custom validator. If there are more than one classes of the type, then one can use the optional <span class=\"code\">serviceQualifier<\/span> argument, which is the qualifier for the bean that we want to fetch. This could be the case, for instance, if the type is <span class=\"code\">UserService<\/span> (interface), and we have the following implementations of this interface: <span class=\"code\">UserServiceOneImpl<\/span> and <span class=\"code\">UserServiceTwoImpl<\/span>.<\/p>\n<p>Now that our service bean has been fetched from the application context, we can use it from within the <span class=\"code\">isValid<\/span> method. Because our service is declared with the static type of an interface, we do not need to know the dynamic type of the bean within our validator class. This is essentially what makes this solution dynamic, as it can therefore be used in various contexts.<\/p>\n<p>Dependencies can be injected into the custom Hibernate validator because by default, the <span class=\"code\">LocalValidatorFactoryBean<\/span> configures a <span class=\"code\">SpringConstraintValidatorFactory<\/span> that uses Spring to create <span class=\"code\">ConstraintValidator<\/span> instances. This allows custom <span class=\"code\">ConstraintValidators<\/span> to benefit from dependency injection like any other Spring bean. This is <a href=\"http:\/\/docs.spring.io\/spring\/docs\/3.0.0.RC3\/reference\/html\/ch05s07.html\" target=\"_blank\" rel=\"external nofollow\">documentered here<\/a> (go to section 5.7.2.2 Configuring Custom Constraints).<\/p>\n<h2>Usage<\/h2>\n<p>To use the validator, we first add the attribute in a class like below.<\/p>\n<pre><code class=\"java\">public class User {\r\n    @Unique(service = UserService.class, fieldName = \"email\", message = \"{email.unique.violation}\")\r\n    private String email;\r\n    \r\n    \/\/ Other fields + getters &amp; setters\r\n}<\/code><\/pre>\n<p>Note that the validator can of course be used in combination with other Hibernate validators, regardless of whether they are standard or custom. Next, we must implement the <span class=\"code\">fieldValueExists<\/span> method of our <span class=\"code\">FieldValueExists<\/span> interface within the <span class=\"code\">UserService<\/span> class. Note that in this case, <span class=\"code\">UserService<\/span> is an interface and the implementation is actually named <span class=\"code\">UserServiceImpl<\/span>, because the dependency is resolved by Spring Framework.<\/p>\n<pre><code class=\"java\">public interface UserService extends FieldValueExists { }<\/code><\/pre>\n<p>Since the above interface extends the <span class=\"code\">FieldValueExists<\/span> interface, we do not need to add the <span class=\"code\">fieldValueExists<\/span> method to our service interface. However, we can of course add more methods if we so desire.<\/p>\n<pre><code class=\"java\">@Service\r\npublic class UserServiceImpl implements UserService {\r\n    @Autowired\r\n    private UserRepository userRepository;\r\n\r\n    @Override\r\n    public boolean fieldValueExists(Object value, String fieldName) throws UnsupportedOperationException {\r\n        Assert.notNull(fieldName);\r\n\r\n        if (!fieldName.equals(&quot;email&quot;)) {\r\n            throw new UnsupportedOperationException(&quot;Field name not supported&quot;);\r\n        }\r\n\r\n        if (value == null) {\r\n            return false;\r\n        }\r\n\r\n        return this.userRepository.existsByEmail(value.toString());\r\n    }\r\n}<\/code><\/pre>\n<p>The logic within the above method can be adapted entirely and freely to fit your needs. In my case, I only wanted to support validating a single field (<span class=\"code\">email<\/span>), hence why an <span class=\"code\">UnsupportedOperationException<\/span> exception is thrown if validation is attempted for other fields. The code invokes a method on a repository to check whether or not the value exists within a database. This repository is a Spring Data JPA repository that is injected into the service by Spring Framework, but you could implement this exactly how you wish. The reason why we must perform one or more checks on the field name is explained below.<\/p>\n<h2>Solution Shortcomings<\/h2>\n<p>Now, I am by no means saying that this solution is flawless, because it is not. Below I will discuss the shortcomings of my solution.<\/p>\n<ol>\n<li>When the validation method on the service is invoked, we must know which field the validation should be done for, because the annotation might be used on multiple fields within a class. This is why the name of the field is required to be passed as an argument in the annotation. This is not pretty, but I was unsuccessful in finding a way to get the field name from the annotation within the validator class.<\/li>\n<li>Because this custom validator is intended to be used in various contexts and for various field types, the static type of the value to validate the uniqueness of is <span class=\"code\">Object<\/span>. This means that we cannot benefit from strong typing within our service implementation, but checking the <span class=\"code\">fieldName<\/span> parameter, we can cast value to a more suitable type. This is a tradeoff that I was personally willing to accept in order to avoid writing many validators with the only difference being the service class. This would be very easy to do, but that can quickly end up becoming a lot of boilerplate code.<\/li>\n<li>By default, Hibernate validates entities just before persisting. Unfortunately, this has to be disabled for this approach to work. This is because at the time of persisting, Hibernate is unaware that it is part of a Spring Framework application, and therefore the dependencies are not injected into the custom validator. However, this would also be the case if the static type of the service field was a specific service implementation rather than an interface. There are <a href=\"http:\/\/stackoverflow.com\/questions\/2712345\/jsr-303-dependency-injection-and-hibernate\" target=\"_blank\" rel=\"external nofollow\">various suggestions on how to make this work<\/a> without disabling entity validation at persist time, but I have been unsuccessful in making it work with Spring Framework 4. For information on how to disable this validation, please see below.<\/li>\n<li>The implementation directly depends on Spring Framework to handle dependency injection, which makes it less portable. This is perfectly fine in my case because I am using Spring Framework, but you might not be. Using it in other contexts should not be too much of a headache, though.<\/li>\n<\/ol>\n<h2>Disabling Hibernate Validation When Persisting Entities<\/h2>\n<p>As previously discussed, I have currently not been able to make this validator work without disabling the validation that Hibernate performs by default when persisting entities. As a result, I have had to disable this behavior. There are several ways of accomplishing this, so I will just show you how I did it by configuring an <span class=\"code\">EntityManagerFactory<\/span> using annotation-based configuration. If you cannot do like this in your particular application, then I kindly ask you to perform a Google search.<\/p>\n<pre><code class=\"java\">@Configuration\r\npublic class PersistenceConfig {\r\n    @Bean\r\n    public EntityManagerFactory entityManagerFactory() throws SQLException {\r\n        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();\r\n        vendorAdapter.setGenerateDdl(false);\r\n        vendorAdapter.setDatabase(Database.POSTGRESQL);\r\n        vendorAdapter.setShowSql(true);\r\n\r\n        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();\r\n        factory.setJpaVendorAdapter(vendorAdapter);\r\n        String[] packagesToScan = new String[] { ... };\r\n        factory.setPackagesToScan(packagesToScan);\r\n        factory.setDataSource(this.dataSource());\r\n        factory.setValidationMode(ValidationMode.NONE); \/\/ &lt;--------\r\n        factory.afterPropertiesSet();\r\n\r\n        return factory.getObject();\r\n    }\r\n}<\/code><\/pre>\n<h2>Final Words<\/h2>\n<p>Please note that validating uniqueness like this does not guarantee that a value is unique, as it is possible for <a title=\"Phantom reads\" href=\"http:\/\/en.wikipedia.org\/wiki\/Isolation_%28database_systems%29#Phantom_reads\" target=\"_blank\" rel=\"external nofollow\">phantom reads<\/a> to occur. Basically this means that just because your validator &#8220;claims&#8221; that a value does not exist for a given field (supposedly by looking it up in a database), it is not guaranteed that the value does not exist when an entity is being persisted. That is why one must also apply a unique constraint to the column within the database to ensure that uniqueness is enforced. This is always good practice, but the chances of facing problems by not doing this are higher for highly concurrent systems.<\/p>\n<p>As discussed above, my solution has a few shortcomings, but I still personally prefer it over other solutions that I have found. That being said, please let me know if you know how to overcome these problems, as I would be very interested to learn how to improve this solution.<\/p>\n<p>I hope this article helped anyone solve this problem. Happy coding and thank you for reading!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are quite a few approaches to validating whether or not a given value already exists in a data store or not. In this article, I will show my solution to this problem by using Hibernate Validator in combination with Spring Framework.\u00a0As you will see, the latter is solely used for injecting a dependency into&hellip; <a href=\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\" class=\"more-link\">read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[97],"tags":[99,102,42,103,98],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Unique Field Validation Using Hibernate and Spring<\/title>\n<meta name=\"description\" content=\"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework&#039;s dependency injection.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Unique Field Validation Using Hibernate and Spring\" \/>\n<meta property=\"og:description\" content=\"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework&#039;s dependency injection.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2015-02-27T22:05:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T17:18:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\",\"url\":\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\",\"name\":\"Unique Field Validation Using Hibernate and Spring\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2015-02-27T22:05:27+00:00\",\"dateModified\":\"2017-06-11T17:18:17+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework's dependency injection.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Unique Field Validation Using Hibernate and Spring\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Unique Field Validation Using Hibernate and Spring","description":"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework's dependency injection.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring","og_locale":"en_US","og_type":"article","og_title":"Unique Field Validation Using Hibernate and Spring","og_description":"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework's dependency injection.","og_url":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2015-02-27T22:05:27+00:00","article_modified_time":"2017-06-11T17:18:17+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring","url":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring","name":"Unique Field Validation Using Hibernate and Spring","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2015-02-27T22:05:27+00:00","dateModified":"2017-06-11T17:18:17+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Learn how to validate that a field is unique by writing a custom Hibernate validator that utilizes Spring Framework's dependency injection.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/java\/hibernate\/unique-field-validation-using-hibernate-spring#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Unique Field Validation Using Hibernate and Spring"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-nP","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1477"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=1477"}],"version-history":[{"count":19,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1477\/revisions"}],"predecessor-version":[{"id":2920,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/1477\/revisions\/2920"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=1477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=1477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=1477"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}