-1

This is a Spring Hibernate implementation. I have defined a custom Type definition where I need to pass

one default value to my custom Type Definition class as below.

But the value is null please help me what am I missing here ?

@TypeDef(name = "customString", typeClass = com.mydomain.EncryptString.class)
public class employee{

    private String empId;
    private String empName;
    @Type(type="customString")
    private String passportNumber;

      //setter and getters 
}


public class EncryptString mplements UserType{
    private String password; // inject via spring configurations

    @Override
    public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {

        String encryptedPassport = rs.getString(names[0]);

        System.out.println(names.length);

        System.out.println("##"+password); // This null ????

        return ""
    }

    //password getters and setters methods

}

Spring configurations

<bean id="customString"
    class="com.mydomain.EncryptString">     
    <property name="password" value="password" />
</bean>

value of the password password in nullSafeGet() method it prints Null. How to make it

inject ? I expect Spring will load all the given default values and it instantiate values

when the EncryptString class call via Hibernate annotation.

Updating my question with findings

I have saw a example where TypeDef pass spring bean id as a parameter as below.

   @TypeDef(name = "encryptedString", typeClass =     
   org.jasypt.hibernate.type.EncryptedStringType.class, parameters = { @Parameter(name 
   = "encryptorRegisteredName", value = "hibernateStringEncryptor") })

Spring configuration is

 <bean id="hibernateStringEncryptor"
    class="org.jasypt.hibernate.encryptor.HibernatePBEStringEncryptor">
    <property name="registeredName" value="hibernateStringEncryptor" />
    <property name="password" value="password" />

    <property name="saltGenerator">         
        <bean class="org.jasypt.salt.FixedStringSaltGenerator">
            <property name="salt" value="salt"/>
        </bean>         
    </property>

hibernateStringEncryptor configuration is injected via type def passing the bean id as a

parameter. I went through the code http://www.jasypt.org/download.html but could not figure

out the way the bean get injected.

user3558691
  • 117
  • 2
  • 12
  • What are you actually trying to do and you need the injection into EncryptString ? There may be a better way to get what you want done, but your example doesn't really express your intent very well – geoand May 23 '14 at 06:16
  • This is just a hypothetical business scenario. Please think only the technical requirement. – user3558691 May 23 '14 at 09:59

1 Answers1

0

I think you need to use @Configurable annotation because Spring doesn't instantiate the custom types, Hibernate does and Spring doesn't know about those instances. This is the section in the documentation that describes the scenario. I haven't used this approach myself, but @Configurable would be the first thing I'd try.

Basically, you need to annotate your EncryptString class with @Configurable, to add spring-aspect.jar to your project's classpath, to add <context:spring-configured/> to your xml config file and to enable AspectJ weaving. The documentation gives quite some details regarding this.

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
  • Thanks. Please refer the ticket after Updating my question with findings heading. Is there a way to pass bean id a Type def parameter and inject it. This is working. When de-compile the jasypt jar could not figureout the way this happen. But it is working. – user3558691 May 23 '14 at 09:58
  • What you added in there is different from what you asked initially. There is a new framework in there that you are using. – Andrei Stefan May 23 '14 at 10:31
  • I looked over the code and I don't think they actually do injection: the `HibernatePBEStringEncryptor` has its field populated by injection, but what happens with `EncryptedStringType` is not Spring injection. – Andrei Stefan May 23 '14 at 10:45
  • In [this code](http://svn.code.sf.net/p/jasypt/code/trunk/jasypt-hibernate3/src/main/java/org/jasypt/hibernate/encryptor/HibernatePBEStringEncryptor.java) when `setRegisteredName()` is called from Spring (inside the xml config file) a SINGLETON instance of `HibernatePBEEncryptorRegistry` is created and the instance Spring created is "registered" with that singleton Registry. – Andrei Stefan May 23 '14 at 10:46
  • If you look at [EncryptedStringType](http://svn.code.sf.net/p/jasypt/code/trunk/jasypt-hibernate3/src/main/java/org/jasypt/hibernate/type/AbstractEncryptedAsStringType.java) whenever `nullSafeGet` or `nullSafeSet` are called the same singleton `HibernatePBEEncryptorRegistry` is checked and the encryptor registered (not injected) earlier - `HibernatePBEStringEncryptor` - is used, encryptor that has been created by Spring. – Andrei Stefan May 23 '14 at 10:49
  • So, they use the Singleton pattern to get access to an instance of a bean created by Spring. This, I believe, is why that works but I may be wrong (I looked over the code for few minutes). – Andrei Stefan May 23 '14 at 10:49
  • Yes got the point. The object setter methods access static instance and add all necessary fields. Accepting your previous answer too as it s also a solution for this issue – user3558691 May 25 '14 at 05:22
  • Hi Andrei, I am facing a similar issue where I have to instruct hibernate to use types without hibernate.cfg.xml. I have posted the question in SO at http://stackoverflow.com/questions/32890694/spring-use-hibernate-types-without-hibernate-cgf-xml . Can you please check it out. Thanks a lot. :-) – We are Borg Oct 01 '15 at 15:21