"convertToDatabaseColumn" 的 JPA 转换器不必要调用导致 ClassCastException

JPA Converter unnessecary call of "convertToDatabaseColumn" leads to ClassCastException

我有一个Converter将存储在DB中的String转换为对应的Enum元素:

@Converter(autoApply=true)
public class DokumentTypConverter implements AttributeConverter<DokumentTypSchluessel, String> {

   @Override
   public String convertToDatabaseColumn(DokumentTypSchluessel attribute) {
      return attribute.getValue();
   }

   @Override
   public DokumentTypSchluessel convertToEntityAttribute(String dbData) {
      return DokumentTypSchluessel.from(dbData);
   }
}

我有一个实体 class,它有一个枚举类型的字段:

@Entity
@Table(name="REF_DOKUMENTTYPEN")
public class DokumentTyp {
   private DokumentTypSchluessel schluessel;
   // snip
}

现在,当我尝试从数据库中检索该实体的实例时

String query = "SELECT d FROM DokumentTyp d WHERE d.schluessel = " + VORBEREITUNG;
entityManager.createQuery(query, DokumentTyp.class).getSingleResult();

我收到以下错误:

javax.persistence.PersistenceException: An exception occurred while calling convertToDatabaseColumn on converter class de.drvbund.pub.model.convert.DokumentTypConverter with value VORBEREITUNG
        at org.eclipse.persistence.mappings.converters.ConverterClass.convertObjectValueToDataValue(ConverterClass.java:139)
        at org.eclipse.persistence.mappings.foundation.AbstractDirectMapping.getFieldValue(AbstractDirectMapping.java:778)
        at org.eclipse.persistence.internal.expressions.QueryKeyExpression.getFieldValue(QueryKeyExpression.java:420)
        at org.eclipse.persistence.internal.expressions.ConstantExpression.printSQL(ConstantExpression.java:152)
        at org.eclipse.persistence.expressions.ExpressionOperator.printDuo(ExpressionOperator.java:2241)
        at org.eclipse.persistence.internal.expressions.CompoundExpression.printSQL(CompoundExpression.java:286)
        at org.eclipse.persistence.internal.expressions.RelationExpression.printSQL(RelationExpression.java:899)
        at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.translateExpression(ExpressionSQLPrinter.java:325)
        at org.eclipse.persistence.internal.expressions.ExpressionSQLPrinter.printExpression(ExpressionSQLPrinter.java:129)
        at org.eclipse.persistence.internal.expressions.SQLSelectStatement.printSQL(SQLSelectStatement.java:1755)
        at org.eclipse.persistence.internal.databaseaccess.DatabasePlatform.printSQLSelectStatement(DatabasePlatform.java:3268)
        at org.eclipse.persistence.platform.database.OraclePlatform.printSQLSelectStatement(OraclePlatform.java:967)
        at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:843)
        at org.eclipse.persistence.internal.expressions.SQLSelectStatement.buildCall(SQLSelectStatement.java:854)
        at org.eclipse.persistence.descriptors.ClassDescriptor.buildCallFromStatement(ClassDescriptor.java:815)
        at org.eclipse.persistence.internal.queries.StatementQueryMechanism.setCallFromStatement(StatementQueryMechanism.java:390)
        at org.eclipse.persistence.internal.queries.StatementQueryMechanism.prepareSelectAllRows(StatementQueryMechanism.java:315)
        at org.eclipse.persistence.internal.queries.ExpressionQueryMechanism.prepareSelectAllRows(ExpressionQueryMechanism.java:1723)
        at org.eclipse.persistence.queries.ReadAllQuery.prepareSelectAllRows(ReadAllQuery.java:904)
        at org.eclipse.persistence.queries.ReadAllQuery.prepare(ReadAllQuery.java:835)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:673)
        ... 151 more
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to de.drvbund.pub.enums.DokumentTypSchluessel

我猜框架从查询中获取了 Enum 元素的 toString() 表示,假设它是实体属性值,需要转换为其相应的 dbValue 并尝试调用传递 StringconvertToDatabaseColumn() 方法。我只是不知道如何正确地做到这一点。

如何使用 JPA 检索按 Enum 属性值过滤的实体?

根据 Chris 的建议解决了问题:

entityManager.createQuery("SELECT d FROM DokumentTyp d WHERE d.schluessel = ?1", DokumentTyp.class)
             .setParameter(1, DokumentTypSchluessel.VORBEREITUNG)
             .getSingleResult();