使用 Spring Data JPA 自动转换参数 [英] Automatically convert a parameter with Spring Data JPA

查看:31
本文介绍了使用 Spring Data JPA 自动转换参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

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

    public String toString() {
       return id;
    }
}

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

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

class SomeEntity {
    @Column
    private String itsID;
}

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

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

public SomeEntity findByItsID(ID itsId);

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

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 将参数转换为预期类型(例如,通过注册 Spring 转换器)?或者,我们是否必须让方法采用这样的字符串:

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:

public SomeEntity findByItsId(String itsId);

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

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

推荐答案

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

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:

实体:

@Entity
class SomeEntity {

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

转换器:

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
    }

}

注意事项:

  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. 在 Spring 上使用 JPA,包含转换器的包必须出现在 EntityManagerFactoryBean 上的属性 packageToScan 中.
  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 packageToScan on EntityManagerFactoryBean.

这篇关于使用 Spring Data JPA 自动转换参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆