title: Unable to get entity using named query on jasypt encrypted column
body: <p>I have an Employee entity having ssn field encrypted using jasypt. Following is a mock definition:</p>
  
  <pre><code>@TypeDef(name = "encryptedString", typeClass = EncryptedStringType.class, parameters = {@Parameter(name = "encryptorRegisteredName",value = "strongHibernateStringEncryptor")})
  @Entity
  @Table(name="employee")
  @NamedQueries(value = {
      @NamedQuery(name = "employee.getEmployeeBySSN", query = "SELECT employee from Employee employee WHERE employee.ssn=:ssn"),
      @NamedQuery(name = "employee.getEmployeeByName", query = "SELECT employee from Employee employee WHERE employee.name=:name")
      })
  public class Employee {
  
      @Id @GeneratedValue
      private Long id;
  
      private String name;
  
      @Type(type = "encryptedString")
      private String ssn;
  }
  </code></pre>
  
  <p>This entity contains two named queries for getting employees, one with name and other with ssn. SSN field is encrypted using jasypt. The code is a mock implementation hence I have used the following basic configuration:</p>
  
  <pre><code>public static void main(String[] args) throws SerialException, SQLException {
  
      //Configure jasypt encryptor
      PooledPBEStringEncryptor strongEncryptor = new PooledPBEStringEncryptor();
      strongEncryptor.setAlgorithm("PBEWITHMD5ANDDES");
      strongEncryptor.setPassword("jasypt");
      strongEncryptor.setPoolSize(2);
  
      //Register it with hibernate
      HibernatePBEEncryptorRegistry registry = HibernatePBEEncryptorRegistry.getInstance();
      registry.registerPBEStringEncryptor("strongHibernateStringEncryptor", strongEncryptor);
  
      //Get an entity manager factory
      EntityManagerFactory emf = Persistence.createEntityManagerFactory("helloworld");
  
      //Get an entity manager
      EntityManager em = emf.createEntityManager();
      EntityTransaction tx = em.getTransaction();
      tx.begin();
  
      //Create an employee
      Employee employee = new Employee();
      employee.setName("Vaibhav");
      employee.setSsn("1234567");
      em.persist(employee);
  
      tx.commit();
  
      EntityTransaction newtx = em.getTransaction();
      newtx.begin();
  
      //Search an employee with ssn
      Query queryObject1 = em.createNamedQuery("employee.getEmployeeBySSN");
      queryObject1.setParameter("ssn", "1234567");
  
      //No results here
      List employees1 = queryObject1.getResultList();
  
      newtx.commit();
      em.close();
  
  }
  </code></pre>
  
  <p>I get no result in <code>employees1</code> list. However when I run the following named query, I'm able to see decrypted ssn in the employee object.</p>
  
  <pre><code>Query queryObject = em.createNamedQuery("employee.getEmployeeByName");
  queryObject.setParameter("name", "Vaibhav");
  List employees = queryObject.getResultList();
  Employee employee1 = (Employee)employees.get(0);
  </code></pre>
  
  <p>I am unable to understand is there a bug in my code or is it how hibernate is supposed to work. 
  In the documentation <a href="http://www.jasypt.org/hibernate.html" rel="nofollow">Integrating Jasypt with Hibernate 3.x or 4.x</a>, it is written that:</p>
  
  <blockquote>
    <p>But encryption sets a limitation on your Hibernate usage: security
    standards establish that two different encryption operations on the
    same data should not return the same value (due to the use of a random
    salt). Because of this, none of the fields that are set to be
    encrypted when persisted can be a part of a WHERE clause in your
    search queries for the entity they belong to.</p>
  </blockquote>
  
  <p>So it means that one cannot perform search operations on an encrypted field.</p>