spring-data-jpa icon indicating copy to clipboard operation
spring-data-jpa copied to clipboard

Add support for compound keys using @IdClass to JpaPersistentEntity [DATAJPA-770]

Open spring-projects-issues opened this issue 10 years ago • 2 comments
trafficstars

Oliver Drotbohm opened DATAJPA-770 and commented

Currently, JpaPersistentEntity assumes that identifiers are represented by a single property. This unfortunately doesn't hold true for use cases that involve @IdClass.

The code we need roughly already exists in JpaMetamodelEntityInformation. The class unfortunately resides in the repository subsystem so the suggested fix here is to move that functionality into a IdentifierAccessor implementation which then can be used from the EntityInformation implementation in turn


Reference URL: http://stackoverflow.com/questions/31816468/spring-data-rest-idclass-not-recognized

6 votes, 11 watchers

spring-projects-issues avatar Aug 05 '15 03:08 spring-projects-issues

Hendy Irawan commented

Related to DATAREST-846

spring-projects-issues avatar Jun 26 '16 14:06 spring-projects-issues

Dario Tortola commented

I think I've come to something that works. In a RepositoryRestConfigurerAdapter add a Lookup

    config.withEntityLookup().forRepository(EmployeeDepartmentRepository.class, (EmployeeDepartment ed) -> {
            EmployeeDepartmentPK pk = new EmployeeDepartmentPK();
            pk.setDepartmentId(ed.getDepartmentId());
            pk.setEmployeeId(ed.getEmployeeId());
            return pk;
        }, EmployeeDepartmentRepository::findOne);

This enables the application to answer the /employeeDepartment request, but the links are not right (they're using the toString method on the EmployeeDepartment object), so write a BackendIdConverter:

    @Bean
    public BackendIdConverter employeeDepartmentIdConverter() {
        return new BackendIdConverter() {

            @Override
            public boolean supports(Class<?> delimiter) {
                return EmployeeDepartment.class.equals(delimiter);
            }

            @Override
            public String toRequestId(Serializable id, Class<?> entityType) {
                EmployeeDepartmentPK pk = (EmployeeDepartmentPK) id;
                return String.format("%s_%s", pk.getEmployeeId(), pk.getDepartmentId());
            }

            @Override
            public Serializable fromRequestId(String id, Class<?> entityType) {
                if (id == null){
                    return null;
                }
                String[] parts = id.split("_");
                EmployeeDepartmentPK pk = new EmployeeDepartmentPK();
                pk.setEmployeeId(parts[0]);
                pk.setDepartmentId(parts[1]);
                return pk;
            }
        };
    }

This is used to write more normal URLs. Anyway, I'll handle this with caution. Since the id fields were String, they could contain _, so the separator should be based on something different, the ids set to numeric o enforce that they can't contain the separator character

I wrote a sample project following the stackoverflow question, it's in https://github.com/dariotortola/jira-datajpa-770, just in case it helps

spring-projects-issues avatar Feb 09 '17 15:02 spring-projects-issues