gpt4 book ai didi

java - 如何忽略单元测试中的某些字段,Hibernate

转载 作者:行者123 更新时间:2023-11-29 02:47:06 28 4
gpt4 key购买 nike

我的实体模型中有以下字段。

@Column(name="key")
@ColumnTransformer(
read="AES_DECRYPT(key, SHA1('passcode'))",
write="AES_ENCRYPT(?, SHA1('passcode'))")
private String secret_key;

我正在使用 MySql 数据库,hibernate 将使用 AES_ENCRYPT 和 AES_DECRYPT 函数完美地加密/解密 key 值。但是,spring 嵌入式数据库(HSQL 或 H2)不知道这个特定的 MySql 函数。它抛出以下错误:

 javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not insert: [com.xxx.Table]
at ...

Caused by: org.hibernate.exception.SQLGrammarException: could not insert: [com.xxx.Table]
at ...

Caused by: java.sql.SQLSyntaxErrorException: user lacks privilege or object not found: AES_ENCRYPT

所以我的问题是,

  1. 我如何告诉 Hibernate 在写入某些数据库时忽略该字段。或者
  2. 如何让 HSQL 和/或 H2 与这个 mysql 函数一起工作。

谢谢

最佳答案

将此类添加到您的测试配置中:

@Component
public class RemoveAesFunction {



@PostConstruct
public void postConstruct() {
setKey(MyEntity.class);
}

private void setKey(Class<?> clazz) {
try {
Field field = clazz.getDeclaredField("firstName");

ColumnTransformer columnTransformer = field.getDeclaredAnnotation(ColumnTransformer.class);
updateAnnotationValue(columnTransformer, "read","");
updateAnnotationValue(columnTransformer, "write","?");
} catch (NoSuchFieldException | SecurityException e) {
throw new RuntimeException();
}
}

@SuppressWarnings("unchecked")
private void updateAnnotationValue(Annotation annotation, String annotationProperty,String value) {
Object handler = Proxy.getInvocationHandler(annotation);
Field merberValuesField;
try {
merberValuesField = handler.getClass().getDeclaredField("memberValues");
} catch (NoSuchFieldException | SecurityException e) {
throw new IllegalStateException(e);
}
merberValuesField.setAccessible(true);
Map<String, Object> memberValues;
try {
memberValues = (Map<String, Object>) merberValuesField.get(handler);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalStateException(e);
}

memberValues.put(annotationProperty, value);
}
}

关于java - 如何忽略单元测试中的某些字段,Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40663546/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com