Spring Data REST @Idclass not recognized

后端 未结 3 1038
[愿得一人]
[愿得一人] 2020-12-03 04:25

I have an entity named EmployeeDepartment as below

@IdClass(EmployeeDepartmentPK.class) //EmployeeDepartmentPK is a serializeable object
@Entity
EmployeeDepa         


        
相关标签:
3条回答
  • 2020-12-03 04:34

    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);
      }
    }
    
    0 讨论(0)
  • 2020-12-03 04:37

    Use @BasePathAwareController to customize Spring data rest controller.

        @BasePathAwareController
        public class CustInfoCustAcctController {
    
        @Autowired
        CustInfoCustAcctRepository cicaRepo;
    
        @RequestMapping(value = "/custInfoCustAccts/{id}", method = RequestMethod.GET)
        public @ResponseBody custInfoCustAccts getOne(@PathVariable("id") String id) {
            String[] parts = id.split("_");
            CustInfoCustAcctKey key = new CustInfoCustAcctKey(parts[0],parts[1]);
            return cicaRepo.getOne(key);
        }
    }
    

    It's work fine for me with sample uri /api/custInfoCustAccts/89232_70

    0 讨论(0)
  • 2020-12-03 04:41

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

    }

    0 讨论(0)
提交回复
热议问题