JAVA形参自动转换,使用Spring Data JPA自动转换参数

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;

}

}

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;

}

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

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.

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);

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

解决方案

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:

@Entity

class SomeEntity {

@Column(name = "my_id_column_name")

@Convert(converter = MyIDConverter.class)

private ID itsID;

}

Converter:

public class MyIDConverter implements AttributeConverter {

@Override

public String convertToDatabaseColumn(ItStaticDataKey javaKey) {

// your implementation here

}

@Override

public ItStaticDataKey convertToEntityAttribute(final String databaseKey) {

// your implementation here

}

}

Notes:

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).

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.

With JPA on Spring, package containing converters have to appear in property packageToScan on EntityManagerFactoryBean.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中,形参是在方法声明中定义的变量。它们用于接收方法调用时传递的实际参数值,并在方法体内使用。以下是Java形参使用方法: 1. 形参的声明:在方法的括号内部,指定形参的类型和名称。例如,`public void printName(String name)`中的`name`就是一个形参,类型为`String`。 2. 形参的传递:当调用方法时,需要传递实际参数值给形参。例如,`printName("John")`中的"John"就是实际参数值,它会被传递给`name`形参。 3. 形参使用:在方法体内部,可以像使用普通变量一样使用形参。可以将其作为方法体内的计算、判断、赋值等操作的操作数或操作对象。 4. 形参的作用域:形参只在方法体内部有效,超出方法体范围后就无法访问。这意味着形参的作用域仅限于该方法。 5. 形参的数量和类型:方法可以有零个或多个形参,并且每个形参都必须具有唯一的名称。此外,形参还可以具有不同的数据类型,根据实际需求进行定义。 例如,下面是一个使用形参的示例方法: ```java public void printName(String name) { System.out.println("Hello, " + name + "!"); } ``` 在上述方法中,`printName`方法接受一个`String`类型的形参`name`,并在方法体内使用它来打印问候语。当调用该方法时,需要传递一个实际的字符串参数值,例如`printName("John")`将打印出"Hello, John!"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值