Spring Data REST @Idclass not recognized

I have an object called EmployeeDepartment as shown below

@IdClass(EmployeeDepartmentPK.class) //EmployeeDepartmentPK is a serializeable object @Entity EmployeeDepartment{ @Id private String employeeID; @Id private String departmentCode; ---- Getters, Setters and other props/columns } 

and I have a Spring data repository as below

 @RepositoryRestResource(....) public interface IEmployeeDepartmentRepository extends PagingAndSortingRepository<EmployeeDepartment, EmployeeDepartmentPK> { } 

In addition, I have a converter registered to convert from String to EmployeeDepartmentPK.

Now for an object qualified by employeeID = "abc123" and departmentCode = "JBG", I expect the identifier to be used when the SDR interface is called, this is abc123_JBG. For example, http: // localhost / EmployeeDepartment / abc123_JBG should bring me the result, and it really is.

But when I try to save the object using PUT, the ID property available in the BasicPersistentEntity Spring Data Commons class has the value abc123_JBG for the department code. This is not true. I am not sure if this is the expected behavior.

Please, help.

Thanks!

+7
spring spring-data spring-data-rest hibernate jpa
source share
2 answers

Currently, Spring Data REST only supports composite keys, which are represented as a single field. This means that only @EmbeddedId . I registered DATAJPA-770 to fix this.

If you can switch to @EmbeddedId , you still need to teach Spring Data REST how you want to represent your complex identifier in a URI and how to convert the path segment back to an instance of your id BackendIdConverter do this, run BackendIdConverter and register it as a 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); } } 
+10
source share

If you cannot use @EmbeddedId , you can use @IdClass . To do this, you need a BackendIdConverter, as Oliver Girke answered, but you also need to add a search 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); } 

}

+2
source share

All Articles