Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * =============================================================================
   * 
   *   Copyright (c) 2007, The JASYPT team (http://www.jasypt.org)
   * 
   *   Licensed under the Apache License, Version 2.0 (the "License");
   *   you may not use this file except in compliance with the License.
   *   You may obtain a copy of the License at
   * 
  *       http://www.apache.org/licenses/LICENSE-2.0
  * 
  *   Unless required by applicable law or agreed to in writing, software
  *   distributed under the License is distributed on an "AS IS" BASIS,
  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  *   See the License for the specific language governing permissions and
  *   limitations under the License.
  * 
  * =============================================================================
  */
 package org.jasypt.hibernate.type;
 
 

A Hibernate 3 UserType implementation which allows encryption of BigDecimal values into String (VARCHAR) database fields 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="encryptedBigDecimalAsString" class="org.jasypt.hibernate.type.EncryptedBigDecimalAsStringType">
      <param name="encryptorRegisteredName">myHibernateStringEncryptor</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="salary" column="SALARY" type="encryptedBigDecimalAsString" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

...where a HibernatePBEStringEncryptor object should have been previously registered to be used from Hibernate with name myHibernateStringEncryptor (see org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor and org.jasypt.hibernate.encryptor.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="encryptedBigDecimalAsString" class="org.jasypt.hibernate.type.EncryptedBigDecimalAsStringType">
      <param name="algorithm">PBEWithMD5AndTripleDES</param>
      <param name="password">XXXXX</param>
      <param name="keyObtentionIterations">1000</param>
    </typedef>
    ...
    <class name="UserData" table="USER_DATA">
      ...
      <property name="salary" column="SALARY" type="encryptedBigDecimalAsString" />
      ...
    <class>
    ...
  <hibernate-mapping>
 

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

Author(s):
Daniel Fernández Garrido
Since:
1.2
public final class EncryptedBigDecimalAsStringType extends AbstractEncryptedAsStringType{

    
    protected Object convertToObject(String string) {
        return new BigDecimal(string);
    }
    public Class returnedClass() {
        return BigDecimal.class;
    }
New to GrepCode? Check out our FAQ X