Spring Data REST @Idclass not recognized
Currently Spring Data REST only supports compound keys that are represented as by a single field. That effectively means only @EmbeddedId
is supported. I've filed DATAJPA-770 to fix that.
If you can switch to @EmbeddedId
you still need to teach Spring Data REST the way you'd like to represent your complex identifier in the URI and how to transform the path segment back into an instance of your id type. To achieve that, implement a BackendIdConverter
and register it as Spring bean.
@Component
class CustomBackendIdConverter implements BackendIdConverter {
@Override
public Serializable fromRequestId(String id, Class<?> entityType) {
// Make sure you validate the input
String[] parts = id.split("_");
return new YourEmbeddedIdType(parts[0], parts[1]);
}
@Override
public String toRequestId(Serializable source, Class<?> entityType) {
YourIdType id = (YourIdType) source;
return String.format("%s_%s", …);
}
@Override
public boolean supports(Class<?> type) {
return YourDomainType.class.equals(type);
}
}
If you can't use @EmbeddedId
, you can still use @IdClass
. For that, you need the BackendIdConverter as Oliver Gierke answered, but you also need to add a Lookup for your domain type:
@Configuration
public class IdClassAllowingConfig extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.withEntityLookup().forRepository(EmployeeDepartmentRepository.class, (EmployeeDepartment ed) -> {
EmployeeDepartmentPK pk = new EmployeeDepartmentPK();
pk.setDepartmentId(ed.getDepartmentId());
pk.setEmployeeId(ed.getEmployeeId());
return pk;
}, EmployeeDepartmentRepository::findOne);
}
}