Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7904251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:58:27+00:00 2026-06-03T09:58:27+00:00

I am in need of a custom constraint which validates that a value is

  • 0

I am in need of a custom constraint which validates that a value is unique. I have been searching the internet for a long time to find a good example. The one I found is using hibernate template which is not recommended anymore so I was wondering if anyone had the expertise to “translate” this code so that it doesn’t use the templates. I have tried to do it myself but I don’t understand what the getter and setter are for. Here is the code which I got from this helpful guide.

package com.omgo.security.domain.validator;

import java.io.Serializable;
import java.util.List;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

public class UniqueIDValidator implements ConstraintValidator<Unique, Serializable> {

HibernateTemplate hibernateTemplate;

private Class<?> entityClass;
private String uniqueField;

public void initialize(Unique unique) {
    entityClass = unique.entity();
    uniqueField = unique.property();
}

public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {

    String query = String.format("from %s where %s = ? ", entityClass.getName(), uniqueField);
    List<?> list = hibernateTemplate.find(query, property);

    return list != null && list.size() > 0;
}

public SessionFactory getSessionFactory() {
    return hibernateTemplate != null ? hibernateTemplate.getSessionFactory() : null;
}

@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
    this.hibernateTemplate = new HibernateTemplate(sessionFactory);
}
}

Thank you for your help.

/D

Update
After changing the code I am getting this error:

org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here

So I’m thinking that maybe I haven’t injected the SessionFactory properly?

UniqueIdValidator.java:

package testapp.mis.validator;
import java.io.Serializable;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; // Added in update 2


public class UniqueIdValidator implements ConstraintValidator<Unique, Serializable> {

@Autowired(required=true)
private SessionFactory sessionFactory;

private Class<?> entityClass;
private String uniqueField;

public void initialize(Unique unique) {
    entityClass = unique.entity();
    uniqueField = unique.property();
}

    @Transactional //Added in update 2 and validation works after that
public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
    Criteria crit = sessionFactory.getCurrentSession().createCriteria(entityClass);
    crit.add(Restrictions.eq(uniqueField, property));
    return crit.list().isEmpty();
}

Update 2
I got it working by adding @Transactional in the code.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:58:30+00:00Added an answer on June 3, 2026 at 9:58 am

    That uses plain HQL, you can achieve something simpler by using the Criteria API, something like:

    public boolean isValid(Serializable property, ConstraintValidatorContext cvContext) {
        Criteria crit = sessionFactory.getCurrentSession().createCriteria(entityClass);
        crit.add(Restrictions.eq(uniqueField, property));
        return crit.list().isEmpty();
    }
    

    That should check for uniqueness.

    Instead of injecting the hibernate template, just inject the SessionFactory which you also should have configured in your spring context.

    Hope you find it useful!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create a custom constraint annotation which can access the value of
I need to have a JToggleButton (that has custom background) that contains a JPanel
Has anyone come across the need to have a constraint that is only applied
I need a custom event that should raise if a new/existing file in a
I need to make custom text field which will format numeric values according to
I have an entity ArticlePattern , which has a property pattern (string). I need
I have a legacy database with a pretty evil design that I need to
Those are array contraints: array have custom size which i will read in top
Suppose I have a custom file format, which can be analogous to N tables.
I have a generic method that works for most of my custom types. Today

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.