Spring数据REST @Idclass无法识别 [英] Spring Data REST @Idclass not recognized

查看:135
本文介绍了Spring数据REST @Idclass无法识别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  @IdClass(EmployeeDepartmentPK.class)// EmployeeDepartmentPK是一个可序列化的对象
@Entity
EmployeeDepartment {

@Id
私人字符串employeeID;

@Id
private String departmentCode;
---- Getters,Setters和其他道具/栏目
}

我有一个Spring Data Repository定义如下

  @RepositoryRestResource(....)
public interface IEmployeeDepartmentRepository扩展PagingAndSortingRepository< EmployeeDepartment,EmployeeDepartmentPK> {

}

另外,我有一个转换器注册从String现在,对于由employeeID =abc123和departmentCode =JBG限定的实体,我期望在SDR接口被调用时使用该ID是abc123_JBG。
例如 http:// localhost / EmployeeDepartment / abc123_JBG 应该能够取得结果,而且确实如此。但是,当我尝试使用PUT保存实体时,Spring Data Commons的BasicPersistentEntity类中可用的ID属性的值为
departmentCode的abc123_JBG 。这是错误的。我不确定这是否是预期的行为。



请帮助。



谢谢!

解决方案

目前,Spring Data REST仅支持以单个字段表示的复合键。这实际上意味着仅支持 @EmbeddedId 。我已经提交了 DATAJPA-770 来解决这个问题。



如果您可以切换到 @EmbeddedId ,您仍然需要按照您希望在URI中表示复杂标识符的方式教授Spring Data REST,并且如何将路径段转换回您的id类型的实例。为了达到这个目的,实现一个 BackendIdConverter 并将它注册为Spring bean。

  @Component 
class CustomBackendIdConverter implements BackendIdConverter {
$ b @Override
public Serializable fromRequestId(String id,Class<> entityType){

/ /确保你验证输入

String [] parts = id.split(_);
返回新的YourEmbeddedIdType(parts [0],parts [1]);

$ b @Override
public String toRequestId(Serializable source,Class<> entityType){

YourIdType id =(YourIdType)source;
return String.format(%s_%s,...);

$ b @Override
public boolean supports(Class<?> type){
返回YourDomainType.class.equals(type);
}
}


I have an entity named EmployeeDepartment as 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 defined as as below

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

}

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

Now, for an entity, qualified by ID employeeID="abc123" and departmentCode="JBG", I expect the ID to use when SDR interface is called is abc123_JBG. For example http://localhost/EmployeeDepartment/abc123_JBG should fetch me the result and indeed it does.

But, when I try to save an entity using PUT, the ID property available in BasicPersistentEntity class of Spring Data Commons is having a value of abc123_JBG for departmentCode. This is wrong. I'm not sure if this is an expected behaviour.

Please help.

Thanks!

解决方案

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

这篇关于Spring数据REST @Idclass无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆