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:
Converter:
Notes:
@Id
.packageToScan
onEntityManagerFactoryBean
.