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

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

Does setter generates hibernate query for Entity class automatically

问题

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

  1. @SuperBuilder
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Getter
  5. @Setter
  6. @ToString(callSuper = true)
  7. @Entity
  8. @Table(name = "operation", schema = "ssp")
  9. public class Operation extends CommonEntity {
  10. @Id
  11. @Column(name = "id")
  12. @GeneratedValue(strategy = GenerationType.IDENTITY)
  13. private Long id;
  14. @ToString.Exclude
  15. @ManyToOne
  16. @JoinColumn(name = "router_ref_id")
  17. private Router router;
  18. @ToString.Exclude
  19. @OneToMany(mappedBy = "operation", cascade = CascadeType.ALL)
  20. @OrderBy("sequence")
  21. private List<OperationQr> qrList;
  22. @ToString.Exclude
  23. @OneToMany(mappedBy = "operation", cascade = CascadeType.ALL)
  24. private Set<OperationDependency> operationDependencies;
  25. @ToString.Exclude
  26. @OneToMany(mappedBy = "operation", cascade = CascadeType.ALL)
  27. @OrderBy("sequence")
  28. private Set<OperationDocument> opnDocumentList;
  29. @ToString.Exclude
  30. @OneToOne
  31. @JoinColumn(name = "operation_interface")
  32. private OperationInterface operationInterface;
  33. @Column(name = "router_operation_id")
  34. private Integer routerOperationId;
  35. @Column(name = "tenant_id")
  36. private String tenantId;
  37. @Column(name = "external_system_sequence")
  38. private String externalSystemSequence;
  39. }
  40. private Operation saveDetails(OperationDetailsViewModel newOperationData, String sso, Router router, Operation operation) {
  41. // 代码省略...
  42. return operation;
  43. }
  44. @Transactional
  45. public Operation addOperation(OperationDetailsViewModel newOperationData, int indexToAdd, String sso, String changeRequestNumber) {
  46. // 代码省略...
  47. return saveDetails(newOperationData, sso, router, operation);
  48. }
  49. private Operation mergeOperation(OperationDetailsViewModel operationDetailsViewModel, int i, String sso,
  50. String changeRequestNumber, Router router, RouterLock routerLock) {
  51. // 代码省略...
  52. return operation;
  53. }
  54. public static void addOperationToIndexWithResequencing(List<Operation> operations,
  55. Operation newOperationWithoutSequence, int indexToAdd) {
  56. // 代码省略...
  57. }
英文:

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.

  1. @SuperBuilder
  2. @AllArgsConstructor
  3. @NoArgsConstructor
  4. @Getter
  5. @Setter
  6. @ToString(callSuper = true)
  7. @Entity
  8. @Table(name = &quot;operation&quot;, schema = &quot;ssp&quot;)
  9. public class Operation extends CommonEntity{
  10. @Id
  11. @Column(name = &quot;id&quot;)
  12. @GeneratedValue(strategy = GenerationType.IDENTITY)
  13. private Long id;
  14. @ToString.Exclude
  15. @ManyToOne
  16. @JoinColumn(name = &quot;router_ref_id&quot;)
  17. private Router router;
  18. @ToString.Exclude
  19. @OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
  20. @OrderBy(&quot;sequence&quot;)
  21. private List&lt;OperationQr&gt; qrList;
  22. @ToString.Exclude
  23. @OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
  24. private Set&lt;OperationDependency&gt; operationDependencies;
  25. @ToString.Exclude
  26. @OneToMany(mappedBy = &quot;operation&quot;, cascade = CascadeType.ALL)
  27. @OrderBy(&quot;sequence&quot;)
  28. private Set&lt;OperationDocument&gt; opnDocumentList;
  29. @ToString.Exclude
  30. @OneToOne
  31. @JoinColumn(name = &quot;operation_interface&quot;)
  32. private OperationInterface operationInterface;
  33. @Column(name = &quot;router_operation_id&quot;)
  34. private Integer routerOperationId;
  35. @Column(name = &quot;tenant_id&quot;)
  36. private String tenantId;
  37. @Column(name = &quot;external_system_sequence&quot;)
  38. private String externalSystemSequence;

The code which saves the Operation and its parts are :

  1. private Operation saveDetails(OperationDetailsViewModel newOperationData, String sso, Router router, Operation operation) {
  2. copyProperties(newOperationData, operation);
  3. StopWatch stopWatch = new StopWatch();
  4. stopWatch.start();
  5. Optional&lt;XRefWorkcenterRequirementProfile&gt; requirementProfile = workCenterService.getRequirementProfileFor( router, newOperationData.getWorkCenter().getId() );
  6. operation.setRequrementProfile(requirementProfile.isPresent() ? requirementProfile.get().getProfileId() : null);
  7. if (isOperationInterfaceChanged(operation.getOperationInterface(), newOperationData.getOperationInterface())) {
  8. OperationInterface newOperationInterface = operationInterfaceService.getOperationInterface(
  9. router.getEnterprise(), router.getBusinessUnit(), router.getPlant(), newOperationData.getOperationInterface().getId());
  10. operation.setOperationInterface(newOperationInterface);
  11. qualityResultService.removeOldQr(operation);
  12. List&lt;OperationQr&gt; qrList = qualityResultService.createQrList(sso, operation, newOperationData.getGlobalQr());
  13. qrList.addAll(qualityResultService.createQrList(sso, operation, newOperationData.getLocalQr()));
  14. operation.setQrList(qrList);
  15. Set&lt;OperationDependency&gt; operationDependencies = operationDependenciesService.createDependencies( sso, operation, newOperationData.getDependencies() );
  16. operationDependencySetValidator.validateDependencySet( operationDependencies );
  17. operationDependenciesService.removeOldDependencies( operation );
  18. operation.setOperationDependencies( operationDependencies );
  19. documentService.removeOldOperationDocuments(operation);
  20. operation.setOpnDocumentList(documentService.createDocuments(sso, operation, newOperationData.getDocuments()));
  21. purchaseDetailService.updatePurchaseDetail(sso, operation, newOperationData.getPurchaseDetails());
  22. addCustomFields( sso, operation, newOperationData ) ;
  23. OperationUtils.setEntityUpdateData( operation, sso );
  24. stopWatch.stop();
  25. LOGGER.info(&quot;..........SaveOperationDetails.......... Total time = &quot; + stopWatch.getTotalTimeMillis() + &quot; ms&quot;);
  26. return operation;
  27. }

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

  1. @Transactional
  2. public Operation addOperation( OperationDetailsViewModel newOperationData, int indexToAdd, String sso, String changeRequestNumber ) {
  3. Router router = getEditableRouter( newOperationData.getRouterId(), sso, changeRequestNumber );
  4. Operation operation = prepareNewOperation(sso, router);
  5. OperationUtils.addOperationToIndexWithResequencing( router.getOperationsList(), operation, indexToAdd );
  6. return saveDetails(newOperationData, sso, router, operation);
  7. }
  8. private Operation mergeOperation(OperationDetailsViewModel operationDetailsViewModel, int i, String sso,
  9. String changeRequestNumber, Router router, RouterLock routerLock) {
  10. Operation operation;
  11. if(operationDetailsViewModel.getId() == null) {
  12. operation = saveOperationDetailsService
  13. .addOperation(operationDetailsViewModel, i, sso, changeRequestNumber);
  14. }
  15. else {
  16. operation = saveOperationDetailsService
  17. .updateOperation(operationDetailsViewModel, sso, changeRequestNumber, router, routerLock);
  18. }
  19. return operation;
  20. }
  21. public static void addOperationToIndexWithResequencing(List&lt;Operation&gt; operations,
  22. Operation newOperationWithoutSequence, int indexToAdd) {
  23. operations.sort(Comparator.comparing(Operation::getSequence));
  24. if (indexToAdd &lt; operations.size()) {
  25. operations.add(indexToAdd, newOperationWithoutSequence);
  26. } else {
  27. operations.add(newOperationWithoutSequence);
  28. }
  29. resequenceOperations(operations);
  30. }

答案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 调用。它会在事务结束时自动保存。

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

  1. Operation operation = prepareNewOperation(sso, router);
  2. 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 -

  1. Operation operation = prepareNewOperation(sso, router);
  2. 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:

确定