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

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

问题描述

我有一个名为 EmployeeDepartment 的实体,如下所示

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
}

我有一个如下定义的 Spring Data Repository

and I have a Spring Data Repository defined as as below

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

}

此外,我注册了一个转换器,可以从 String 转换为 EmployeeDepartmentPK.

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

现在,对于一个由 ID employeeID="abc123" 和 departmentCode="JBG" 限定的实体,我希望在调用 SDR 接口时使用的 ID 是 abc123_JBG.例如 http://localhost/EmployeeDepartment/abc123_JBG 应该获取我的结果,确实如此.

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.

但是,当我尝试使用 PUT 保存实体时,Spring Data Commons 的 BasicPersistentEntity 类中可用的 ID 属性值为部门代码的 abc123_JBG.这是错误的.我不确定这是否是预期的行为.

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.

请帮忙.

谢谢!

推荐答案

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

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.

如果您可以切换到 @EmbeddedId,您仍然需要教 Spring Data REST 您希望在 URI 中表示复杂标识符的方式以及如何将路径段转换回实例您的 id 类型.为此,请实现一个 BackendIdConverter 并将其注册为 Spring bean.

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 Data REST @Idclass 无法识别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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