0
休眠加载此实体后,我需要为指定的字段(带有自定义注释)做一些额外的业务逻辑。所以,我创建了一个像这样的hibernate拦截器。但是让我困惑的是我无法获得注释信息。以下代码中的encryptAnnotation始终为空。从休眠拦截器获取实体字段注释
public class HibernateInterceptor extends EmptyInterceptor {
public boolean onLoad(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {
for (int i = 0; i < types.length; i++) {
Type type = types[i];
if (type instanceof StringType) {
StringType stringType = (StringType) types[i];
Encrypt encryptAnnotation = stringType.getJavaTypeDescriptor().getJavaTypeClass().getAnnotation(Encrypt.class);
if (encryptAnnotation != null) {
//todo: decrypt field
return true;
}
}
}
return false;
}
}
这里是实体和注释的定义:
@Entity
@Table(name = "table_name")
public class Trade implements Serializable {
@Encrypt
private String shiptoAddr;
}
@Target({ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Encrypt {
}
当然,你不能对“实体”做任何事情。您正在检查将永远不会具有该注释的类型描述符的类。您必须检查实体的字段以获取注释。 –
你是对的。谢谢。 – scott