title: Spring : Use Hibernate types without hibernate.cgf.xml
body: <p>I am working on a Spring-MVC application which uses Hibernate as an ORM tool. We would like to encrypt some columns and for that I am using Jasypt. Now, for that I have to use the annotation @Type, but it requires that there should be a hibernate.cfg.xml file present. Unfortunately, I am not using hibernate.cfg.xml and my sessionFactory and all other configuration are defined in applicationServletContext.xml and root-context.xml.</p>
  
  <p>These are the types I want to use : Filename : jasyptHibernateTypes.hbm.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;
  
  &lt;hibernate-mapping&gt;
  
      &lt;!-- VARCHAR, CLOB, TEXT based types --&gt;
  
      &lt;typedef name="encryptedString" class="org.jasypt.hibernate.type.EncryptedStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedBigDecimalAsString" class="org.jasypt.hibernate.type.EncryptedBigDecimalAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedBigIntegerAsString" class="org.jasypt.hibernate.type.EncryptedBigIntegerAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedBooleanAsString" class="org.jasypt.hibernate.type.EncryptedBooleanAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedByteAsString" class="org.jasypt.hibernate.type.EncryptedByteAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedCalendarAsString" class="org.jasypt.hibernate.type.EncryptedCalendarAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedDateAsString" class="org.jasypt.hibernate.type.EncryptedDateAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedDoubleAsString" class="org.jasypt.hibernate.type.EncryptedDoubleAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedFloatAsString" class="org.jasypt.hibernate.type.EncryptedFloatAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedIntegerAsString" class="org.jasypt.hibernate.type.EncryptedIntegerAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedLongAsString" class="org.jasypt.hibernate.type.EncryptedLongAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedShortAsString" class="org.jasypt.hibernate.type.EncryptedShortAsStringType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
  
      &lt;!-- VARBINARY, BLOB based type --&gt;
      &lt;typedef name="encryptedBinary" class="org.jasypt.hibernate.type.EncryptedBinaryType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptByteHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;!-- NUMERIC, NUMBER based types --&gt;
      &lt;typedef name="encryptedBigDecimal" class="org.jasypt.hibernate.type.EncryptedBigDecimalType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasyptBigDecimalHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
      &lt;typedef name="encryptedBigInteger" class="org.jasypt.hibernate.type.EncryptedBigIntegerType"&gt;
          &lt;param name="encryptorRegisteredName"&gt;jasypBigIntegertHibernateEncryptor&lt;/param&gt;
      &lt;/typedef&gt;
  
  
  
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>Here is my root-context.xml :</p>
  
  <pre><code>&lt;context:component-scan base-package="com.journaldev.spring"&gt;
          &lt;context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/&gt;
      &lt;/context:component-scan&gt;
  
      &lt;context:property-placeholder location="classpath:application.properties"/&gt;
  
      &lt;beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
                  destroy-method="close"&gt;
          &lt;beans:property name="driverClassName" value="org.postgresql.Driver"/&gt;
          &lt;beans:property name="url"
                          value="jdbc:postgresql://localhost:5432/dbname"/&gt;
          &lt;beans:property name="username" value="postgres"/&gt;
          &lt;beans:property name="password" value="password"/&gt;
          &lt;beans:property name="removeAbandoned" value="true"/&gt;
          &lt;beans:property name="removeAbandonedTimeout" value="20"/&gt;
          &lt;beans:property name="defaultAutoCommit" value="false"/&gt;
      &lt;/beans:bean&gt;
  
      &lt;!-- Hibernate 4 SessionFactory Bean definition --&gt;
      &lt;beans:bean id="hibernate4AnnotatedSessionFactory"
                  class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"&gt;
          &lt;beans:property name="dataSource" ref="dataSource"/&gt;
          &lt;beans:property name="packagesToScan" value="com.journaldev.spring.model"/&gt;
  
          &lt;beans:property name="hibernateProperties"&gt;
              &lt;beans:props&gt;
                  &lt;beans:prop key="hibernate.dialect"&gt;org.hibernate.dialect.PostgreSQL9Dialect&lt;/beans:prop&gt;
                  &lt;beans:prop key="hibernate.show_sql"&gt;false&lt;/beans:prop&gt;
                  &lt;!--   &lt;beans:prop key="hibernate.jdbc.batch_size"&gt;1000&lt;/beans:prop&gt;
                     &lt;beans:prop key="hibernate.order_updates"&gt;true&lt;/beans:prop&gt;--&gt;
                  &lt;beans:prop key="hibernate.hbm2ddl.auto"&gt;update&lt;/beans:prop&gt;
                  &lt;beans:prop key="cache.use_second_level_cache"&gt;true&lt;/beans:prop&gt;
                  &lt;beans:prop key="cache.use_query_cache"&gt;true&lt;/beans:prop&gt;
              &lt;/beans:props&gt;
          &lt;/beans:property&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="LoginServiceImpl" class="com.journaldev.spring.service.LoginServiceImpl"/&gt;
  
      &lt;task:annotation-driven/&gt;
  
      &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt;
  
      &lt;beans:bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"&gt;
          &lt;beans:property name="sessionFactory" ref="hibernate4AnnotatedSessionFactory"/&gt;
      &lt;/beans:bean&gt;
  
   &lt;beans:bean id="stringEncryptor"  class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor" lazy-init="false"&gt;
          &lt;beans:property name="algorithm" value="PBEWithMD5AndDES" /&gt;
          &lt;beans:property name="password" value="password" /&gt;
      &lt;/beans:bean&gt;
  
      &lt;beans:bean id="hibernateEncryptor" class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor" lazy-init="false"&gt;
          &lt;!-- This property value must match "encryptorRegisteredName" used when defining hibernate user types --&gt;
          &lt;beans:property name="registeredName" value="jasyptHibernateEncryptor" /&gt;
          &lt;beans:property name="encryptor" ref="stringEncryptor" /&gt;
      &lt;/beans:bean&gt;
  </code></pre>
  
  <p>As you can see, my major config is in this file, and I have also put the interceptors here. I am getting this error :</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: Could not determine type for: encryptedString
      at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:516)
      at org.hibernate.cfg.SetSimpleValueTypeSecondPass.doSecondPass(SetSimpleValueTypeSecondPass.java:42)
      at org.hibernate.cfg.Configuration.processSecondPassesOfType(Configuration.java:1472)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1846)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
      ... 98 more
  Caused by: org.hibernate.annotations.common.reflection.ClassLoadingException: Unable to load Class [encryptedString]
      at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:60)
      at org.hibernate.cfg.annotations.SimpleValueBinder.fillSimpleValue(SimpleValueBinder.java:497)
      ... 108 more
  Caused by: java.lang.ClassNotFoundException: encryptedString
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
      at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
      at java.lang.Class.forName0(Native Method)
      at java.lang.Class.forName(Class.java:348)
      at org.hibernate.annotations.common.util.StandardClassLoaderDelegateImpl.classForName(StandardClassLoaderDelegateImpl.java:57)
      ... 109 more
  </code></pre>
  
  <p>Model class:</p>
  
  <pre><code> @Column(name = "mnotetext")
      @Type(type="encryptedString")
      private String mnotetext;
  
      @Column(name = "mnoteheadline")
      @Type(type="encryptedString")
      private String mnotetag;
  </code></pre>
  
  <p>What should I do to use the types without hibernate.cfg.xml. How to instruct hibernate that they are in this xml file. Thanks a lot. :-)</p>
  
  <p><em>Update</em></p>
  
  <p>Model  class :</p>
  
  <pre><code>@TypeDef( name = "registeredName",
          defaultForType = org.jasypt.hibernate.type.EncryptedStringType.class,
          typeClass = org.jasypt.hibernate.type.EncryptedStringType.class)
  @Entity
  @Table(name = "groupnotes")
  public class GroupNotes implements Serializable{
   @Column(name = "mnotetext")
      @Type(type="registeredName")
      private String mnotetext;
  
      @Column(name = "mnoteheadline")
      @Type(type="registeredName")
      private String mnotetag;
  }
  </code></pre>
  
  <p>New error log :</p>
  
  <pre><code>Caused by: org.hibernate.MappingException: Unable to instantiate custom type: org.jasypt.hibernate.type.EncryptedStringType
      at org.hibernate.type.TypeFactory.custom(TypeFactory.java:218)
      at org.hibernate.type.TypeFactory.custom(TypeFactory.java:204)
      at org.hibernate.type.TypeFactory.byClass(TypeFactory.java:103)
      at org.hibernate.type.TypeResolver.heuristicType(TypeResolver.java:130)
      at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:340)
      at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:322)
      at org.hibernate.mapping.Property.isValid(Property.java:241)
      at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:496)
      at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
      at org.hibernate.cfg.Configuration.validate(Configuration.java:1360)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1851)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1930)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:372)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:454)
      at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:439)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570)
      ... 98 more
  Caused by: org.jasypt.exceptions.EncryptionInitializationException: If "encryptorRegisteredName" is not specified, then "password" (and optionally "algorithm" and "keyObtentionIterations") must be specified
      at org.jasypt.hibernate.type.AbstractEncryptedAsStringType.setParameterValues(AbstractEncryptedAsStringType.java:226)
      at org.hibernate.type.TypeFactory.injectParameters(TypeFactory.java:131)
      at org.hibernate.type.TypeFactory.custom(TypeFactory.java:214)
      ... 114 more
  </code></pre>