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
H

Harpia

@Harpia
About
Posts
1
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How to use additional parameters inside ConstraintValidator.isValid method
    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; 
    

    }
    }

    Java database xml tutorial
  • Login

  • Don't have an account? Register

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