Java SpringBootAPI Rest与DTO和@manytoone关系-最佳实践

Java SpringBootAPI Rest与DTO和@manytoone关系-最佳实践,java,spring,spring-boot,spring-data,dto,Java,Spring,Spring Boot,Spring Data,Dto,我有以下配置: ProductPriceEntity @Entity @Table(name = "PRODUCTPRICE") public class ProductPriceEntity { ... @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "SUPERMARKET_STORE_ID", nullable = false) private Super

我有以下配置:

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

@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);

}
这是可行的,但我无法返回DTO。ProductPriceResponse上的超市和商店为空

然后我改变了存储属性关系的级联

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SUPERMARKET_STORE_ID", nullable = false)
private SupermarketStoreEntity store;
我得到了这个错误:

“传递到persist的分离实体:model.SupermarketStoreEntity;嵌套异常为org.hibernate.persistentoobjectexception:传递到persist的分离实体:model.SupermarketStoreEntity”

这是有道理的。Modelmapper将长storeId转换为仅具有id和分离的SupermarketStoreEntity

最后我的问题是:什么是最佳实践?

我是否应该获取storeId,而不是转换为分离的SupermarketStoreEntity,并在ProductPriceControllerImpl上找到具有您的storeId的SupermarketStoreEntity

或者不是。我是否应该删除级联并在保存ProductPriceEntity后获取保存的ProductPriceEntity?我怀疑store和supermarket仍然会变为null,因为cascade不在那里


谢谢大家

猜测最佳实践是引入@Service类,其中有一个带有@Transactional注释的方法。服务被注入控制器,实体的所有加载和保存都在控制器中进行。这将有助于解决分离错误

我不太喜欢cascade属性,我更喜欢在代码中做这件事——但这更像是一种观点


也许您想看看-您可以在那里用REST api定义数据库,并了解控制器和服务的最佳实践。

我已经使用了一个带有事务的服务类和一个带有事务的方法。。。。但我的问题依然存在!如果我使用CascadeType.ALL(或CascadeType.PERSIST),我得到了分离错误。。。。如果我不使用Cascade,它可以正常工作,但我不会得到存储详细信息,因为没有Cascade。我不知道!我应该在持久化之前获取存储实体吗?我只想在持久化之前传递存储的id和Spring数据获取存储实体。。。。。或者在……之后。。。。对于我的响应…听起来分离来自实际的级联:您删除了实体,并且由于级联,StoreDetails也被删除。现在,您拥有的StoreDetails已分离,因为它不再存在。我将尝试使用Cascade NONE来实现这一点,并分别删除相关实体。但是很难远程帮助你。
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "SUPERMARKET_STORE_ID", nullable = false)
private SupermarketStoreEntity store;