简体   繁体   English

如何使用MockMvc更新多对多关系的实体?

[英]How to update entity with many to many relation using MockMvc?

I am writing simple spring-boot app and want to test it. 我正在编写简单的spring-boot app并想测试它。 I have User class: 我有User类:

@Entity
@Table(name = "users")
@NoArgsConstructor
@AllArgsConstructor
public class User extends AbstractEntity {
    public enum Type {
        ADMIN, USER
    }

    @Getter
    @Setter
    private Type type;

    @Getter
    @Setter
    private String username;

    @Getter
    @Setter
    private String password;

    @ManyToMany(mappedBy = "managers")
    @Getter
    @Setter
    private Set<Label> label;
}

And Label class (consider it's an organization, where User works): Label类(认为它是User工作的组织):

@Entity
@Table(name = "labels")
@NoArgsConstructor
@AllArgsConstructor
public class Label extends AbstractEntity {
    @Getter
    @Setter
    private String title;

    @OneToOne
    @Getter
    @Setter
    private User creator;

    @ManyToMany(cascade = CascadeType.ALL)
    @JoinTable(name = "label_managers")
    @Getter
    @Setter
    private Set<User> managers;
}

Those entities are managed by @RepositoryRestResource s. 这些实体由@RepositoryRestResource管理。

What should I write in my test with MockMvc to add relation between a User and a Label ? 我应该在MockMvc测试中写MockMvc如何在UserLabel之间添加关系?

"By using "Content-Type: text/uri-list" instead of JSON, it is possible to "add" a resource to the collection with a PUT and pass in the URI. You can remove the resource with a DELETE." “通过使用”Content-Type:text / uri-list“而不是JSON,可以使用PUT将资源”添加“到集合中并传入URI。您可以使用DELETE删除资源。”

Review the answer to the following question: How to add elements in a many-to-many relationship via Spring's @RepositoryRestResource REST API? 回顾以下问题的答案: 如何通过Spring的@RepositoryRestResource REST API在多对多关系中添加元素?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM