微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

setter是否会自动为Entity类生成休眠查询

如何解决setter是否会自动为Entity类生成休眠查询

我在使用Lombok的Hibernate JPA Spring Boot应用程序中有一个实体类。在分析代码时,我找不到任何save()代码。设置程序是否会自动生成休眠查询并将其保存到数据库

@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString(callSuper = true)
@Entity
@Table(name = "operation",schema = "ssp")
public class Operation extends CommonEntity{
    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ToString.Exclude
    @ManyToOne
    @JoinColumn(name = "router_ref_id")
    private Router router;

    @ToString.Exclude
    @OnetoMany(mappedBy = "operation",cascade = CascadeType.ALL)
    @OrderBy("sequence")
    private List<OperationQr> qrList;

    @ToString.Exclude
    @OnetoMany(mappedBy = "operation",cascade = CascadeType.ALL)
    private Set<OperationDependency> operationDependencies;

    @ToString.Exclude
    @OnetoMany(mappedBy = "operation",cascade = CascadeType.ALL)
    @OrderBy("sequence")
    private Set<OperationDocument> opnDocumentList;

    @ToString.Exclude
    @OnetoOne
    @JoinColumn(name = "operation_interface")
    private OperationInterface operationInterface;

    @Column(name = "router_operation_id")
    private Integer routerOperationId;

    @Column(name = "tenant_id")
    private String tenantId;

    @Column(name = "external_system_sequence")
    private String externalSystemSequence;

保存Operation及其部分的代码为:

private Operation saveDetails(OperationDetailsviewmodel newOperationData,String sso,Router router,Operation operation) {
    copyProperties(newOperationData,operation);
    
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();

    Optional<XRefWorkcenterRequirementProfile> requirementProfile = workCenterService.getRequirementProfileFor( router,newOperationData.getWorkCenter().getId() );
    
    operation.setRequrementProfile(requirementProfile.isPresent() ? requirementProfile.get().getProfileId() : null);
    
    if (isOperationInterfaceChanged(operation.getoperationInterface(),newOperationData.getoperationInterface())) {
        OperationInterface newOperationInterface = operationInterfaceService.getoperationInterface(
                router.getEnterprise(),router.getBusinessUnit(),router.getPlant(),newOperationData.getoperationInterface().getId());
        operation.setoperationInterface(newOperationInterface);
    qualityResultService.removeOldQr(operation);
    List<OperationQr> qrList = qualityResultService.createQrList(sso,operation,newOperationData.getGlobalQr());
    qrList.addAll(qualityResultService.createQrList(sso,newOperationData.getLocalQr()));
    operation.setQrList(qrList);

    Set<OperationDependency> operationDependencies = operationDependenciesService.createDependencies( sso,newOperationData.getDependencies() );
    operationDependencySetValidator.validateDependencySet( operationDependencies );
    operationDependenciesService.removeOldDependencies( operation );
    operation.setoperationDependencies( operationDependencies );

    documentService.removeOldOperationDocuments(operation);
    operation.setopnDocumentList(documentService.createDocuments(sso,newOperationData.getDocuments()));

    purchaseDetailService.updatePurchaseDetail(sso,newOperationData.getPurchaseDetails());
    addCustomFields( sso,newOperationData ) ;

    OperationUtils.setEntityUpdateData( operation,sso );
    
    stopWatch.stop();
    LOGGER.info("..........SaveOperationDetails.......... Total time = " + stopWatch.getTotalTimeMillis() + " ms");

    return operation;
}

saveDetails()包装在另一种方法@Transactional

 @Transactional
    public Operation addOperation( OperationDetailsviewmodel newOperationData,int indexToAdd,String changeRequestNumber ) {
        Router router = getEditableRouter( newOperationData.getRouterId(),sso,changeRequestNumber );
        Operation operation = prepareNewOperation(sso,router);
        OperationUtils.addOperationToIndexWithResequencing( router.getoperationsList(),indexToAdd );
        return saveDetails(newOperationData,router,operation);
    }




private Operation mergeOperation(OperationDetailsviewmodel operationDetailsviewmodel,int i,String changeRequestNumber,RouterLock routerLock) {
    Operation operation;

    if(operationDetailsviewmodel.getId() == null) {
        operation = saveOperationDetailsService
            .addOperation(operationDetailsviewmodel,i,changeRequestNumber);
    }
    else {
        operation = saveOperationDetailsService
            .updateOperation(operationDetailsviewmodel,changeRequestNumber,routerLock);
    }

    return operation;
}




  public static void addOperationToIndexWithResequencing(List<Operation> operations,Operation newOperationWithoutSequence,int indexToAdd) {
    operations.sort(Comparator.comparing(Operation::getSequence));

    if (indexToAdd < operations.size()) {
      operations.add(indexToAdd,newOperationWithoutSequence);
    } else {
      operations.add(newOperationWithoutSequence);
    }

    resequenceOperations(operations);
  }

解决方法

“设置程序是否自动生成休眠查询并将其保存到数据库。-否

基本上save()存在于CrudRepository上。 JpaRepository扩展了CrudRepository。因此,它继承了CrudRepository的所有方法。检查文档链接。

https://docs.spring.io/spring-data/jpa/docs/current/api/org/springframework/data/jpa/repository/JpaRepository.html

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html

,

如果Operation实体是受托管的实体(可以从某些jpa存储库(例如findOne等)中检索到),则不需要显式的save或persistent调用。它会在交易结束时自动保存。

我相信,您必须在此处某处进行存储库调用-

Operation operation = prepareNewOperation(sso,router);
OperationUtils.addOperationToIndexWithResequencing( router.getOperationsList(),operation,indexToAdd );

此处类似的答案-https://stackoverflow.com/a/46708295/11244881

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。