简体   繁体   English

具有 DTO 和 @manytoone 关系的 Spring Boot API Rest - 最佳实践

[英]Spring Boot API Rest with DTO and @manytoone relationship - best practice

I have the following configuration:我有以下配置:

ProductPriceEntity产品价格实体

@Entity
@Table(name = "PRODUCTPRICE")
public class ProductPriceEntity {

    ...

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "SUPERMARKET_STORE_ID", nullable = false)
    private SupermarketStoreEntity store;

    ...
}

ProductPriceNewRequest产品价格新请求

@Setter @Getter
public class ProductPriceNewRequest {

    ...

    private Long storeId;

    ...

}

ProductPriceControllerImpl产品价格控制器Impl

@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ResponseEntity<ProductPriceResponse> save(@PathVariable(value = "product_id", required = true) Long productId, @RequestBody @Valid ProductPriceNewRequest productPriceNewRequest) {
    ProductEntity productEntity = productService.findById(productId);

    ProductPriceEntity productPriceEntity = modelMapper.map(productPriceNewRequest, ProductPriceEntity.class);

    productPriceEntity.setProduct(productEntity);
    productPriceEntity = service.insert(productPriceEntity);

    URI location = ServletUriComponentsBuilder.fromCurrentRequest()
            .path("/{id}")
            .buildAndExpand(productPriceEntity.getId())
            .toUri();


    ProductPriceResponse productPriceResponse = modelMapper.map(productPriceEntity, ProductPriceResponse.class);

    return ResponseEntity.created(location).body(productPriceResponse);

}

ProductPriceResponse产品价格反应

@Getter @Setter
public class ProductPriceResponse {

    ...

    private String supermarket;
    private String store;

    ...
}

It's works but I can't to make return DTO.它有效,但我无法返回 DTO。 Supermarket and store are null on ProductPriceResponse. Supermarket 和 store 在 ProductPriceResponse 上为 null。

在此处输入图片说明

Well, then I changed cascade of store attribute relationship.嗯,那我改了级联store属性关系。

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SUPERMARKET_STORE_ID", nullable = false)
private SupermarketStoreEntity store;

And I got this error:我收到了这个错误:

"detached entity passed to persist: model.SupermarketStoreEntity; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: model.SupermarketStoreEntity" “传递给持久化的分离实体:model.SupermarketStoreEntity;嵌套异常是 org.hibernate.PersistentObjectException:传递给持久化的分离实体:model.SupermarketStoreEntity”

That makes sense.那讲得通。 Modelmapper convert a long storeId to a SupermarketStoreEntity with just only id and detached ... Modelmapper 将一个长 storeId 转换为一个 SupermarketStoreEntity,只有 id 和分离的 ...

finally my question: What is best practice?最后我的问题是:什么是最佳实践?

Should I get storeId and doesn't convert to a SupermarketStoreEntity detached and find SupermarketStoreEntity with your storeId on ProductPriceControllerImpl?我应该获取 storeId 并且不转换为分离的 SupermarketStoreEntity 并在 ProductPriceControllerImpl 上找到您的 storeId 的 SupermarketStoreEntity 吗?

Or not.或不。 Should I to remove the cascade and after save ProductPriceEntity I will should fetch ProductPriceEntity saved?我应该删除级联并在保存 ProductPriceEntity 之后我应该获取 ProductPriceEntity 保存吗? I suspect that store and supermarket still will get null because the cascade not there.我怀疑商店和超市仍然会变空,因为级联不在那里。

thank you all谢谢你们

Guess best practice is to introduce an @Service class, where you have a method with @Transactional annotation.猜猜最佳实践是引入一个@Service 类,在那里你有一个带有@Transactional 注释的方法。 The service is injected into the controller and all loading and saving of entities happens there.服务被注入控制器,所有实体的加载和保存都发生在那里。 That should help with the detach-error.这应该有助于解决分离错误。

I'm not a big fan of the cascade attribute and would favor doing this stuff in the code - but that's more an opinion.我不是级联属性的忠实粉丝,并且更喜欢在代码中做这些事情 - 但这更多是一种意见。

Maybe you want to have a look at https://bootify.io - you can define your database with REST api there and get a sense of best practices for controllers and services.:)也许您想看看https://bootify.io - 您可以在那里使用 REST api 定义您的数据库,并了解控制器和服务的最佳实践。:)

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

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