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