简体   繁体   English

Spring Data Rest域驱动设计-发布非聚合根实体

[英]Spring data rest Domain Driven Design - Posting non aggregate root entities

I am making an on-line auction REST api using spring boot data REST. 我正在使用Spring Boot数据REST制作在线拍卖REST API。 I am trying to use the Domain Driven Design appoach. 我正在尝试使用域驱动设计方法。

I have 2 entities....Listing and ListedItem, where Listed item is the item for sale and Listing is composed of the ListedItem and holds some other data about when the listing starts and ends, among other things. 我有2个实体。...Listing和ListedItem,其中Listed项目是待售项目,Listing由ListedItem组成,并且包含一些有关何时开始和结束上市的其他数据。

I am feeling that the Listing must be the aggregate root in this situation so that will control the ListedItem and if I remove the listing the item is removed too. 我感觉清单在这种情况下必须是聚合根,这样就可以控制ListedItem,如果我删除清单,该项目也将被删除。

So I have a repository for the aggregate root (Listing). 因此,我为聚合根(清单)提供了一个存储库。

I will need to POST my ListedItem first so that I can then POST a listing with its linked ListedItem. 我将需要首先发布我的ListedItem,以便随后可以发布带有链接的ListedItem的列表。

How can I now POST a ListedItem using spring data rest? 现在如何使用Spring Data Rest发布ListedItem? No endpoint is exposed for this as it has no repository of its own. 没有端点为此暴露,因为它没有自己的存储库。

I would expect to be able to POST ListedItem to /api/listed-item but I can't work out how to do this when using ddd if I only have a repository for each aggregate route. 我希望能够将ListedItem POST到/ api / listed-item,但是如果我只有每个聚合路由的存储库,则在使用ddd时无法解决如何执行此操作。

Surely ListedItem needs its own repository if I am top persist it? 如果我坚持不懈,肯定ListedItem需要自己的存储库吗?

Here are my entities and repository in case it helps: 如果有帮助,以下是我的实体和存储库:

@Entity
@Table(name = "listed_item")
@Getter
@ToString
@EqualsAndHashCode
public class ListedItem extends BaseEntityModel {

    private String name;

    private String shortDescription;

    private String fullDescription;

}

@Entity
@Table(name = "listing")
@Getter
@ToString
@EqualsAndHashCode
public class Listing extends BaseEntityModel {

    @OneToOne
    private ListedItem listedItem;

    @Enumerated
    private PossibleListingState currentState;

    private long numBids;

    public Listing() {

    }

    public PossibleState getCurrentState() {
        return currentState;
    }

    public void setCurrentState(PossibleListingState currentState) {
        this.currentState = currentState;
    }
}



@RepositoryRestResource(collectionResourceRel = "listings", itemResourceRel = "listing")
public interface ListingRepository extends PagingAndSortingRepository<Listing, String> {
}

I will need to POST my ListedItem first so that I can then POST a listing with its linked ListedItem. 我将需要首先发布我的ListedItem,以便随后可以发布带有链接的ListedItem的列表。

This is a misconception. 这是一个误解。 If Listing is the aggregate root and cannot exist without it's ListedItem entity then both have to be created at the same time. 如果Listing是聚合根,并且没有它的ListedItem实体就不能存在,则必须同时创建两者。 Therefore, you'd most likely just POST to a /listings resource with the necessary data to create both, the Listing and it's ListedItem at once. 因此,您很可能只需将POST到具有必要数据的/listings资源中,即可同时创建Listing及其ListedItem

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

相关问题 域驱动设计中的实体 - Entities in domain driven design Spring Data Rest - 如何防止PUT / PATCH更新到聚合根的子实体 - Spring Data Rest - How to prevent PUT/PATCH updates to child entities of aggregate root 域驱动设计,包含实体和NHibernate持久性 - Domain Driven Design, Containing Entities and NHibernate Persistence Java客户端,用于将复杂的实体发布到Spring Data REST / HATEOAS服务 - Java Client for POSTing complex entities to a Spring Data REST / HATEOAS service Hibernate Spring OneToMany - ManyToOne领域驱动设计 - Hibernate Spring OneToMany - ManyToOne Domain Driven Design Spring环境中的域驱动设计和事务 - Domain driven design and transactions in Spring environment Spring Data Rest:与非导出实体相关的POST实体 - Spring Data Rest: POSTing entity with relation to non-exported entity 在域驱动设计中,事务是否可以修改多个聚合? - In Domain Driven Design, may a transaction modify more than one aggregate? 使用Spring Data在单个请求中发布子实体 - Posting child entities in a single request with Spring Data 使用Spring Data Common发布域事件时如何处理没有存储库的聚合根 - How to deal with the aggregate root without repository, when using Spring Data Common to publish domain events
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM