Java 使用 Spring Data JPA 自动转换参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22965178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:40:24  来源:igfitidea点击:

Automatically convert a parameter with Spring Data JPA

javaspringjpaspring-dataspring-data-jpa

提问by Jan Thom?

In our entity beans we use a custom ID format which includes a checksum to verify that the ID is actually valid. Ids look like ID827391738979. To make sure that all code uses correct IDs only we created a code wrapper around an ID String:

在我们的实体 bean 中,我们使用包含校验和的自定义 ID 格式来验证 ID 是否实际有效。Id 看起来像ID827391738979. 为了确保所有代码只使用正确的 ID,我们围绕 ID 字符串创建了一个代码包装器:

class ID {
    public ID(String id) {
       // parse and verify ID
    }

    public String toString() {
       return id;
    }
}

All code just uses this IDobject. However in our entity we have defined the ID as a String:

所有代码只使用这个ID对象。但是在我们的实体中,我们将 ID 定义为 a String

class SomeEntity {
    @Column
    private String itsID;
}

Now we want to use Spring-Data-JPA to query some object by it's id. So we do:

现在我们想使用 Spring-Data-JPA 通过它的 id 查询某个对象。所以我们这样做:

public SomeEntity findByItsID(ID itsId);

Problem is, that now Spring Data JPA tries to assign the ID-typed parameter to the query, while the query of course expects a String.

问题是,现在 Spring Data JPA 尝试将 ID 类型的参数分配给查询,而查询当然需要一个String.

Is it possible to have Spring Data JPA convert the parameter to the expected type (e.g. by registering a Spring converter) before it is inserted into the query? Or, do we have to let the method take a String like this:

是否可以在将参数插入查询之前让 Spring Data JPA 将参数转换为预期类型(例如,通过注册 Spring 转换器)?或者,我们是否必须让该方法采用这样的字符串:

public SomeEntity findByItsId(String itsId);

I would rather avoid this, so all code can use the IDclass instead of resorting to Strings.

我宁愿避免这种情况,因此所有代码都可以使用ID该类而不是诉诸字符串。

采纳答案by falsarella

You could use a custom type in your entity with JPA 2.1 @Convert, so JPA makes the conversion for you (I've tested it with Spring Data JPA also, and it worked transparently!), see:

您可以在带有 JPA 2.1 的实体中@Convert使用自定义类型,因此 JPA 为您进行转换(我也使用 Spring Data JPA 对其进行了测试,并且它透明地工作!),请参阅:

Entity:

实体:

@Entity
class SomeEntity {

    @Column(name = "my_id_column_name")
    @Convert(converter = MyIDConverter.class)
    private ID itsID;
}

Converter:

转换器:

public class MyIDConverter implements AttributeConverter<ID, String> {

    @Override
    public String convertToDatabaseColumn(ItStaticDataKey javaKey) {
        // your implementation here
    }

    @Override
    public ItStaticDataKey convertToEntityAttribute(final String databaseKey) {
        // your implementation here
    }

}

Notes:

笔记:

  1. Make sure you're using a JPA-2.1-compatible Spring Data JPA version. (I'm sure that Spring Data JPA 1.7.0 (and above) are (or at least should) be compatible).
  2. Unfortunately, it won't work if that field should also be annotated with @Id.
  3. If you use EclipseLink, you have to define converter in persistence.xml, or it doesn't pick it up. https://bugs.eclipse.org/bugs/show_bug.cgi?id=412454.
  4. With JPA on Spring, package containing converters have to appear in property packageToScanon EntityManagerFactoryBean.
  1. 确保您使用的是兼容 JPA-2.1 的 Spring Data JPA 版本。(我确定 Spring Data JPA 1.7.0(及更高版本)是(或至少应该)兼容的)。
  2. 不幸的是,如果该字段也应该用@Id.
  3. 如果使用EclipseLink,则必须在persistence.xml 中定义转换器,否则它不会选择它。https://bugs.eclipse.org/bugs/show_bug.cgi?id=412454
  4. 随着JPA春,包含转换器必须出现在属性packageToScanEntityManagerFactoryBean