设置器是否会自动生成用于实体类的Hibernate查询

huangapple go评论60阅读模式
英文:

Does setter generates hibernate query for Entity class automatically

问题

我在Hibernate JPA Spring Boot应用程序中使用Lombok创建了一个实体类。在分析代码时,我找不到任何save()代码。是不是setter自动生成Hibernate查询并将它们保存到数据库中。

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

private Operation saveDetails(OperationDetailsViewModel newOperationData, String sso, Router router, Operation operation) {
    // 代码省略...
    return operation;
}

@Transactional
public Operation addOperation(OperationDetailsViewModel newOperationData, int indexToAdd, String sso, String changeRequestNumber) {
    // 代码省略...
    return saveDetails(newOperationData, sso, router, operation);
}

private Operation mergeOperation(OperationDetailsViewModel operationDetailsViewModel, int i, String sso,
    String changeRequestNumber, Router router, RouterLock routerLock) {
    // 代码省略...
    return operation;
}

public static void addOperationToIndexWithResequencing(List<Operation> operations,
    Operation newOperationWithoutSequence, int indexToAdd) {
    // 代码省略...
}
英文:

I have an entity class in Hibernate JPA spring boot app using lombok. While analysing the code , I could not find any save() code. Does the setter automatically generates the Hibernate queries and saves them to DB.

@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@ToString(callSuper = true)
@Entity
@Table(name = &quot;operation&quot;, schema = &quot;ssp&quot;)
public class Operation extends CommonEntity{
@Id
@Column(name = &quot;id&quot;)
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ToString.Exclude
@ManyToOne
@JoinColumn(name = &quot;router_ref_id&quot;)
private Router router;
@ToString.Exclude
@OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
@OrderBy(&quot;sequence&quot;)
private List&lt;OperationQr&gt; qrList;
@ToString.Exclude
@OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
private Set&lt;OperationDependency&gt; operationDependencies;
@ToString.Exclude
@OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
@OrderBy(&quot;sequence&quot;)
private Set&lt;OperationDocument&gt; opnDocumentList;
@ToString.Exclude
@OneToOne
@JoinColumn(name = &quot;operation_interface&quot;)
private OperationInterface operationInterface;
@Column(name = &quot;router_operation_id&quot;)
private Integer routerOperationId;
@Column(name = &quot;tenant_id&quot;)
private String tenantId;
@Column(name = &quot;external_system_sequence&quot;)
private String externalSystemSequence;

The code which saves the Operation and its parts are :

private Operation saveDetails(OperationDetailsViewModel newOperationData, String sso, Router router, Operation operation) {
copyProperties(newOperationData, operation);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Optional&lt;XRefWorkcenterRequirementProfile&gt; 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&lt;OperationQr&gt; qrList = qualityResultService.createQrList(sso, operation, newOperationData.getGlobalQr());
qrList.addAll(qualityResultService.createQrList(sso, operation, newOperationData.getLocalQr()));
operation.setQrList(qrList);
Set&lt;OperationDependency&gt; operationDependencies = operationDependenciesService.createDependencies( sso, operation, newOperationData.getDependencies() );
operationDependencySetValidator.validateDependencySet( operationDependencies );
operationDependenciesService.removeOldDependencies( operation );
operation.setOperationDependencies( operationDependencies );
documentService.removeOldOperationDocuments(operation);
operation.setOpnDocumentList(documentService.createDocuments(sso, operation, newOperationData.getDocuments()));
purchaseDetailService.updatePurchaseDetail(sso, operation, newOperationData.getPurchaseDetails());
addCustomFields( sso, operation, newOperationData ) ;
OperationUtils.setEntityUpdateData( operation, sso );
stopWatch.stop();
LOGGER.info(&quot;..........SaveOperationDetails.......... Total time = &quot; + stopWatch.getTotalTimeMillis() + &quot; ms&quot;);
return operation;
}

saveDetails() is wrapped in another method which is @Transactional

 @Transactional
public Operation addOperation( OperationDetailsViewModel newOperationData, int indexToAdd, String sso, String changeRequestNumber ) {
Router router = getEditableRouter( newOperationData.getRouterId(), sso, changeRequestNumber );
Operation operation = prepareNewOperation(sso, router);
OperationUtils.addOperationToIndexWithResequencing( router.getOperationsList(), operation, indexToAdd );
return saveDetails(newOperationData, sso, router, operation);
}
private Operation mergeOperation(OperationDetailsViewModel operationDetailsViewModel, int i, String sso,
String changeRequestNumber, Router router, RouterLock routerLock) {
Operation operation;
if(operationDetailsViewModel.getId() == null) {
operation = saveOperationDetailsService
.addOperation(operationDetailsViewModel, i, sso, changeRequestNumber);
}
else {
operation = saveOperationDetailsService
.updateOperation(operationDetailsViewModel, sso, changeRequestNumber, router, routerLock);
}
return operation;
}
public static void addOperationToIndexWithResequencing(List&lt;Operation&gt; operations,
Operation newOperationWithoutSequence, int indexToAdd) {
operations.sort(Comparator.comparing(Operation::getSequence));
if (indexToAdd &lt; operations.size()) {
operations.add(indexToAdd, newOperationWithoutSequence);
} else {
operations.add(newOperationWithoutSequence);
}
resequenceOperations(operations);
}

答案1

得分: 1

"setter会自动生成Hibernate查询并将它们保存到数据库吗?- 不会

基本上,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"

英文:

"Does the setter automatically generates the Hibernate queries and saves them to DB. - NO

Basically save() is present on CrudRepository. JpaRepository extends the CrudRepository. Hence it inherits all the methods from CrudRepository. Check the docs link.

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

答案2

得分: 1

如果操作实体是托管实体(例如,它是从某个 JPA 存储库中检索的,比如 findOne 等),那么不需要显式的 save 或 persist 调用。它会在事务结束时自动保存。

我相信,在这里某处您肯定有一个存储库调用 -

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

类似的答案在这里 - https://stackoverflow.com/a/46708295/11244881

英文:

If Operation entity is a managed entity (it is retrieved from some jpa repository, for example findOne etc), then explicit save or persist call is not needed. It gets automatically saved at the end of a transaction.

I believe, you must be having a repository call somewhere here -

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

Similar answer here - https://stackoverflow.com/a/46708295/11244881

huangapple
  • 本文由 发表于 2020年9月30日 03:19:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64126234.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定