首页 文章

Spring Boot - 如何在单个POST请求中创建实体和嵌套实体

提问于
浏览
1

我试图用Spring Data弄清楚具体的事情,那就是:如何用一个POST请求创建一个实体和一个相关的实体 .

我可以创建具有单独请求的相关实体,但是单个请求我不确定 . (没有创建特定的控制器方法) . 例:

ReusableComponent.java

@Entity
public class ReusableComponent {
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       private Long id;

       @OneToMany(mappedBy="component", cascade = CascadeType.ALL)
       private List<Consumer> consumers = new ArrayList<>();

       private String name;

       public Long getId() {
              return id;
       }
       public void setId(Long id) {
              this.id = id;
       }
       public List<Consumer> getConsumers() {
              return consumers;
       }
       public void setConsumers(List<Consumer> consumers) {
              this.consumers = consumers;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
}

Consumer.java

@Entity
public class Consumer<ResuableComponent> {
       @Id
       @GeneratedValue(strategy=GenerationType.IDENTITY)
       private Long id;

    private String name;

       @ManyToOne(targetEntity=ReusableComponent.class)
       private ResuableComponent component;

       public Long getId() {
              return id;
       }
       public void setId(Long id) {
              this.id = id;
       }
       public ResuableComponent getComponent() {
              return component;
       }
       public void setComponent(ResuableComponent component) {
              this.component = component;
       }
       public String getName() {
              return name;
       }
       public void setName(String name) {
              this.name = name;
       }
}

ReusableComponentRepository.java

public interface ReusableComponentRepository extends CrudRepository<ReusableComponent, Long> 
{
       Iterable<ReusableComponent> findAll();

       @RestResource(exported = false)
       List<ReusableComponent> findByName(String name);
}

POST http://localhost:8080/reusableComponents

请求机构:

{ "name": "datagrid", "consumers": [{ "name": "financial app" }] }

GET http://localhost:8080/reusableComponents

响应:

{
    "_embedded": {
        "reusableComponents": [
            {
                "consumers": [],
                "name": null,
                "_links": {
                    "self": {
                        "href": "http://localhost:8080/reusableComponents/1"
                    },
                    "reusableComponent": {
                        "href": "http://localhost:8080/reusableComponents/1"
                    }
                }
            }
        ]
    },
    "_links": {
        "self": {
            "href": "http://localhost:8080/reusableComponents"
        },
        "profile": {
            "href": "http://localhost:8080/profile/reusableComponents"
        }
    }
}

如您所见,reusableComponent实例中没有使用者 . 我以前有这个工作,但对于我的生活,我现在无法理解 .

1 回答

  • 0

    我通过删除ReusableComponent实体的consumers属性上的@OneToMany装饰器中的 mappedBy="component" 属性来实现它,如下所示:

    @OneToMany(cascade = CascadeType.ALL)
    private List<Consumer> consumers = new ArrayList<>();
    

    以上工作完美 .

相关问题