Spring Data Rest @EmbeddedId不能从Post Request中构造 [英] Spring Data Rest @EmbeddedId cannot be constructed from Post Request

查看:94
本文介绍了Spring Data Rest @EmbeddedId不能从Post Request中构造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JPA实体 Person 和一个实体 Team .两者都由实体 PersonToTeam 加入.该加入实体与 Person 有多对一的关系,而与 Team 则有多对一的关系.它具有由 Person Team 的ID组成的多列密钥,该ID由@EmbeddedId表示.要将嵌入式ID来回转换为请求ID,我有一个转换器.所有这些都遵循 Spring Data REST @Idclass无法识别

I have a JPA entity Person and an entity Team. Both are joined by an entity PersonToTeam. This joining entity holds a many-to-one relation to Person and one to Team. It has a multi-column key consisting of the ids of the Person and the Team, which is represented by an @EmbeddedId. To convert the embedded id back and forth to the request id I have a converter. All this follows the suggestion on Spring Data REST @Idclass not recognized

代码如下:

@Entity
public class PersonToTeam {
    @EmbeddedId
    @Getter
    @Setter
    private PersonToTeamId id = new PersonToTeamId();

    @ManyToOne
    @Getter
    @Setter
    @JoinColumn(name = "person_id", insertable=false, updatable=false)
    private Person person;

    @ManyToOne
    @Getter
    @Setter
    @JoinColumn(name = "team_id", insertable=false, updatable=false)
    private Team team;

    @Getter
    @Setter
    @Enumerated(EnumType.STRING)
    private RoleInTeam role;

    public enum RoleInTeam {
        ADMIN, MEMBER
    }
}

    @EqualsAndHashCode
    @Embeddable
    public class PersonToTeamId implements Serializable {
        private static final long serialVersionUID = -8450195271351341722L;
        @Getter
        @Setter
        @Column(name = "person_id")
        private String personId;

        @Getter
        @Setter
        @Column(name = "team_id")
        private String teamId;
    }

@Component
public class PersonToTeamIdConverter implements BackendIdConverter {

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

    @Override
    public Serializable fromRequestId(String id, Class<?> entityType) {
        if (id != null) {
            PersonToTeamId ptid = new PersonToTeamId();
            String[] idParts = id.split("-");
            ptid.setPersonId(idParts[0]);
            ptid.setTeamId(idParts[1]);
            return ptid;
        }
        return BackendIdConverter.DefaultIdConverter.INSTANCE.fromRequestId(id, entityType);
    }

    @Override
    public String toRequestId(Serializable id, Class<?> entityType) {
        if (id instanceof PersonToTeamId) {
            PersonToTeamId ptid = (PersonToTeamId) id;
            return String.format("%s-%s", ptid.getPersonId(), ptid.getTeamId());
        }
        return BackendIdConverter.DefaultIdConverter.INSTANCE.toRequestId(id, entityType);
    }
}

此转换器的问题在于,当发布请求尝试创建新的personToTeam关联时,fromRequestId方法将获得null作为id参数.但是没有有关该帖子的有效负载的其他信息.那么,应该如何创建带有该人和团队外键的ID?还有一个更普遍的问题:在春季数据休息中处理多对多关联的正确方法是什么?

The problem with this converter is, that the fromRequestId method gets a null as id parameter, when a post request tries to create a new personToTeam association. But there is no other information about the payload of the post. So how should an id with foreign keys to the person and the team be created then? And as a more general question: What is the right approach for dealing many-to-many associations in spring data rest?

推荐答案

遇到相同的问题后,我找到了解决方案.您的代码应该没问题,但如果fromRequestId()中的idnull,则我返回new PersonToTeamId()而不是DefaultIdConverter.

After running into the same issue I found a solution. Your code should be fine, except I return new PersonToTeamId() instead of the DefaultIdConverter if id is null in fromRequestId().

假设您在帖子请求中使用JSON,则必须将personIdteamId包装在id对象中:

Assuming you are using JSON in your post request you have to wrap personId and teamId in an id object:

{
  "id": {
    "personId": "foo",
    "teamId": "bar"
  },
  ...
}

@EmbeddedId的一部分不是简单数据类型而是外键的情况下:

And in cases where a part of the @EmbeddedId is not a simple data type but a foreign key:

{
  "id": {
    "stringId": "foo",
    "foreignKeyId": "http://localhost:8080/path/to/other/resource/1"
  },
  ...
}

这篇关于Spring Data Rest @EmbeddedId不能从Post Request中构造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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