Class EncryptedBigDecimalType

Object
org.jasypt.hibernate6.type.EncryptedBigDecimalType
All Implemented Interfaces:
org.hibernate.usertype.ParameterizedType, org.hibernate.usertype.UserType

public final class EncryptedBigDecimalType extends Object implements org.hibernate.usertype.UserType, org.hibernate.usertype.ParameterizedType

A Hibernate UserType implementation which allows transparent encryption of BigDecimal values during persistence of entities.

This class is intended only for declarative use from a Hibernate mapping file. Do not use it directly from your .java files (although of course you can use it when mapping entities using annotations).

To use this Hibernate type in one of your Hibernate mappings, you can add it like this:

  <hibernate-mapping package="myapp">
    ...
    <typedef name="encryptedBigDecimal" class="org.jasypt.hibernate.type.EncryptedBigDecimalType">
      <param name="encryptorRegisteredName">myHibernateBigDecimalEncryptor</param>
      <param name="decimalScale">2</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="salary" column="SALARY" type="encryptedBigDecimal" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

...where a HibernatePBEBigDecimalEncryptor object should have been previously registered to be used from Hibernate with name myHibernateBigDecimalEncryptor (see HibernatePBEBigDecimalEncryptor and HibernatePBEEncryptorRegistry).

Or, if you prefer to avoid registration of encryptors, you can configure your encryptor directly in the mapping file (although not recommended), like this:

  <hibernate-mapping package="myapp">
    ...
    <typedef name="encryptedBigDecimal" class="org.jasypt.hibernate.type.EncryptedBigDecimalType">
      <param name="algorithm">PBEWithMD5AndTripleDES</param>
      <param name="password">XXXXX</param>
      <param name="keyObtentionIterations">1000</param>
      <param name="decimalScale">2</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="address" column="ADDRESS" type="encryptedBigDecimal" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

About the decimalScale parameter

The decimalScale parameter is aimed at setting the scale with which BigDecimal numbers will be set to and retrieved from the database. It is an important parameter because many DBMSs return BigDecimal numbers with a scale equal to the amount of decimal positions declared for the field (e.g. if we store "18.23" (scale=2) in a DECIMAL(15,5) field, we can get a "18.23000" (scale=5) back when we retrieve the number). This can affect correct decryption of encrypted numbers, but specifying a decimalScale parameter will solve this issue.

So, if we set decimalScale to 3, and we store "18.23", this Hibernate type will send "18.230" to the encryptor, which is the value that we will get back from the database at retrieval time (a scale of "3" will be set again on the value obtained from DB). If it is necessary, a DOWN rounding operation is executed on the number.


To learn more about usage of user-defined types, please refer to the Hibernate Reference Documentation.

Since:
1.9.0
Author:
Chus Picos
  • Constructor Details

    • EncryptedBigDecimalType

      public EncryptedBigDecimalType()
  • Method Details

    • sqlTypes

      public int[] sqlTypes()
    • returnedClass

      public Class returnedClass()
      Specified by:
      returnedClass in interface org.hibernate.usertype.UserType
    • equals

      public boolean equals(Object x, Object y) throws org.hibernate.HibernateException
      Specified by:
      equals in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • deepCopy

      public Object deepCopy(Object value) throws org.hibernate.HibernateException
      Specified by:
      deepCopy in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • assemble

      public Object assemble(Serializable cached, Object owner) throws org.hibernate.HibernateException
      Specified by:
      assemble in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • disassemble

      public Serializable disassemble(Object value) throws org.hibernate.HibernateException
      Specified by:
      disassemble in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • isMutable

      public boolean isMutable()
      Specified by:
      isMutable in interface org.hibernate.usertype.UserType
    • hashCode

      public int hashCode(Object x) throws org.hibernate.HibernateException
      Specified by:
      hashCode in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • replace

      public Object replace(Object original, Object target, Object owner) throws org.hibernate.HibernateException
      Specified by:
      replace in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
    • nullSafeGet

      public Object nullSafeGet(ResultSet rs, String[] names, org.hibernate.engine.spi.SharedSessionContractImplementor session, Object owner) throws org.hibernate.HibernateException, SQLException
      Throws:
      org.hibernate.HibernateException
      SQLException
    • nullSafeSet

      public void nullSafeSet(PreparedStatement st, Object value, int index, org.hibernate.engine.spi.SharedSessionContractImplementor session) throws org.hibernate.HibernateException, SQLException
      Specified by:
      nullSafeSet in interface org.hibernate.usertype.UserType
      Throws:
      org.hibernate.HibernateException
      SQLException
    • setParameterValues

      public void setParameterValues(Properties parameters)
      Specified by:
      setParameterValues in interface org.hibernate.usertype.ParameterizedType
    • getSqlType

      public int getSqlType()
      Specified by:
      getSqlType in interface org.hibernate.usertype.UserType
    • nullSafeGet

      public Object nullSafeGet(ResultSet resultSet, int i, org.hibernate.engine.spi.SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws SQLException
      Specified by:
      nullSafeGet in interface org.hibernate.usertype.UserType
      Throws:
      SQLException