Is it possible for a JPA AttributeConverter to know anything about what Entity is running on?

Viewed 977

The interface for AttributeConverter is as follows:

public interface AttributeConverter<X,Y> {
    public Y convertToDatabaseColumn (X attribute);
    public X convertToEntityAttribute (Y dbData);
}

In my implementation, I'd like to know a little about the Entity and the Field of the Entity the converter is running on. For instance: should this Converter decrypt this field for the application or not?

JAX-RS has the concept of @Context which is pretty handy. Curious if JPA has an equivalent concept.

2 Answers

I don't think you can do it with plain JPA convertors (see this). But you can do it with Hibernate custom types which implement the DynamicParameterizedType interface.

And simple example:

import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDateTime;
import java.util.Objects;
import java.util.Properties;

import javax.persistence.Column;

import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.DynamicParameterizedType;
import org.hibernate.usertype.UserType;

public class MyConverter implements UserType, DynamicParameterizedType
{
   private int sqlType;
   private Object entity; // com.example.hibernate.Account
   private int columnLength;

   public MyConverter()
   {
   }

   @Override
   public void setParameterValues(Properties parameters)
   {
      sqlType = getSqlType(parameters.get(RETURNED_CLASS));

      Object entity = parameters.get(ENTITY);
      ParameterType reader = (ParameterType) parameters.get(PARAMETER_TYPE);
      this.columnLength = getLength(reader);
   }

   private int getSqlType(Object field)
   {
      if (field instanceof Long) return Types.BIGINT;
      if (field instanceof LocalDateTime) return Types.TIMESTAMP;
      return Types.VARCHAR;
   }

   private int getLength(ParameterType reader)
   {
      int length = -1; // default length
      for (Annotation annotation : reader.getAnnotationsMethod()){
         if (annotation instanceof Column) {
            length =  ((Column) annotation).length();
         }
      }
      return length;
   }

   @Override
   public int[] sqlTypes()
   {
      return new int[] {sqlType};
   }

   @Override
   public Class<?> returnedClass()
   {
      return String.class;
   }

   @Override
   public boolean equals(Object x, Object y) throws HibernateException
   {
      return Objects.equals(x, y);
   }

   @Override
   public int hashCode(Object x) throws HibernateException
   {
      return Objects.hashCode(x);
   }

   @Override
   public Object nullSafeGet(ResultSet rs,
         String[] names,
         SharedSessionContractImplementor session,
         Object owner) throws HibernateException, SQLException 
   {
      Object result = rs.getObject(names[0]);

      if (result instanceof Timestamp) {
         return ((Timestamp) result).toLocalDateTime();
      }

      return result;
   }

   @Override
   public void nullSafeSet(PreparedStatement st,
         Object value,
         int index,
         SharedSessionContractImplementor session) throws HibernateException, SQLException
   {
      if (value == null) {
         st.setNull(index, sqlType);
      }

      if (value instanceof LocalDateTime) {
         st.setTimestamp(index, Timestamp.valueOf((LocalDateTime) value));
      }

      if (value instanceof Long) {
         st.setLong(index, (Long) value);
      }

      if (value instanceof String) {
        st.setString(index, (String)value);
      }
   }

   @Override
   public Object deepCopy(Object value) throws HibernateException
   {
      return value;
   }

   @Override
   public boolean isMutable()
   {
      return false;
   }

   @Override
   public Serializable disassemble(Object value) throws HibernateException
   {
      return Objects.toString(value);
   }

   @Override
   public Object assemble(Serializable cached, Object owner) throws HibernateException
   {
      return cached;
   }

   @Override
   public Object replace(Object original, Object target, Object owner) throws HibernateException
   {
      return original;
   }
}

and usage:

@Entity
public class Account
{
   @Id
   @Type(type = "com.example.hibernate.MyConverter")
   @Column(name = "acc_id")
   private Long id;

   @Column(name = "acc_name", length = 50)
   @Type(type = "com.example.hibernate.MyConverter")
   private String name;

   @Column(name = "acc_regdt")
   @Type(type = "com.example.hibernate.MyConverter")
   private LocalDateTime regDate;
}

You could probably use an entity listener for that purpose:

public class FooEntityListener {

    @PreUpdate
    public void encrypt(Foo foo) {
        ...
    }

    @PostLoad
    public void decrypt(Foo foo) {
        ...
    }
}
@Entity
@EntityListeners(class = FooEntityListener.class)
public class Foo {
    ...
}

And, in case you use Spring, you can perform dependency injection in the entity listener:

@Component
public class FooEntityListener {

    private SomeSortOfSpringBean bean;

    @Autowired
    public FooEntityListener(SomeSortOfSpringBean bean){
        this.bean = bean;
    }

    @PreUpdate
    public void encrypt(Foo foo) {
        ...
    }
}

I haven't tested, but with CDI and JPA 2.1, you should also be able to perform dependency injection in the entity listener.

Related