Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. How to use additional parameters inside ConstraintValidator.isValid method

How to use additional parameters inside ConstraintValidator.isValid method

Scheduled Pinned Locked Moved Java
databasexmltutorial
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Harpia
    wrote on last edited by
    #1

    Good afternoon! I'm trying to create a custom generic Bean Validator for the email of an author and the name of a category to verify that they are unique and I wish I could use a custom JPQL, the name of the persistence unit in persistence.xml and other variables. However, I don't know how to pass variables inside isValid, I only know how to hardcode them.

    public class UniqueFieldValidator implements ConstraintValidator {

    @Override
    public boolean isValid(T value, ConstraintValidatorContext context)
    {
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("project");
    EntityManager em = emf.createEntityManager();

      String jpql = "select a from Author a where a.email = :pEmail";
    
      TypedQuery query = em.createQuery(jpql, Author.class);
      query.setParameter("pEmail", value);
    
      Optional optAuthor = Optional.of(query.getSingleResult());
    
      if(optAuthor.isPresent())
      {
         return false; 
      } 
      
      return true; 
    

    }
    }

    M 1 Reply Last reply
    0
    • H Harpia

      Good afternoon! I'm trying to create a custom generic Bean Validator for the email of an author and the name of a category to verify that they are unique and I wish I could use a custom JPQL, the name of the persistence unit in persistence.xml and other variables. However, I don't know how to pass variables inside isValid, I only know how to hardcode them.

      public class UniqueFieldValidator implements ConstraintValidator {

      @Override
      public boolean isValid(T value, ConstraintValidatorContext context)
      {
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("project");
      EntityManager em = emf.createEntityManager();

        String jpql = "select a from Author a where a.email = :pEmail";
      
        TypedQuery query = em.createQuery(jpql, Author.class);
        query.setParameter("pEmail", value);
      
        Optional optAuthor = Optional.of(query.getSingleResult());
      
        if(optAuthor.isPresent())
        {
           return false; 
        } 
        
        return true; 
      

      }
      }

      M Offline
      M Offline
      Member_15288963
      wrote on last edited by
      #2

      Default Is Valid Implementation As Below public boolean isValid(Object value, ConstraintValidatorContext ctx)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups