英文:
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 = "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;
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<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, operation, newOperationData.getLocalQr()));
operation.setQrList(qrList);
Set<OperationDependency> 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("..........SaveOperationDetails.......... Total time = " + stopWatch.getTotalTimeMillis() + " ms");
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<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);
}
答案1
得分: 1
"setter会自动生成Hibernate查询并将它们保存到数据库吗?- 不会
基本上,save()
方法存在于CrudRepository 上。JpaRepository 继承自 CrudRepository。因此,它继承了CrudRepository的所有方法。请查阅文档链接。
英文:
"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.
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论