gpt4 book ai didi

java - 为什么无法使用 JPA 更改实体的引用对象?

转载 作者:行者123 更新时间:2023-12-01 17:43:39 27 4
gpt4 key购买 nike

我有一个应该编辑的工单对象。在我的票证对象中,有引用对象的属性(见下文)。

...
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "project_id", referencedColumnName="id")
private Project project;

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "costCenter_id", referencedColumnName="id")
private CostCenter costCenter;
...

但是当我尝试更新实体时,我总是收到错误:

Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

@PutMapping("/tickets/{id}")
@ResponseStatus(HttpStatus.OK)
public Ticket updateTicket(@RequestBody Ticket ticket) throws Exception{
Optional<Ticket> o = this.ticketRepo.findById(ticket.getId());
o.ifPresent(element -> {
if(ticket.getCostCenter() != null) {
Optional<CostCenter> c = this.costCenterRepo.findById(ticket.getCostCenter().getId());
c.ifPresent( costCenter -> {
element.setCostCenter(costCenter);
});
}
if(ticket.getProject() != null) {
Optional<Project> p = this.projectRepo.findById(ticket.getProject().getId());
p.ifPresent(project -> {
element.setProject(project);
});
}
this.ticketRepo.save(element);
});
return o.orElseThrow(() -> new NotFoundException(ticket.getId()));
}

PS:当我触发更新而不进行任何更改时,一切正常。

堆栈跟踪:https://textsaver.flap.tv/lists/2vm5

class AuditorAwareImpl implements AuditorAware<Long> {

@Override
public Optional<Long> getCurrentAuditor() {
PersonRepository personRepo = ApplicationContextProvider.getApplicationContext().getBean(PersonRepository.class);
if(SecurityContextHolder.getContext().getAuthentication() != null) {
Person p = personRepo.findByUserPrincipalName(SecurityContextHolder.getContext().getAuthentication().getName() + "@email.com");
return Optional.of(p.getId());
} else {
Person p = personRepo.findByUserPrincipalName("SYSTEM");
return Optional.of(p.getId());
}
}
}

@Component(value = "applicationContextProvider")
class ApplicationContextProvider implements ApplicationContextAware {

private static class AplicationContextHolder {
private static final InnerContextResource CONTEXT_PROV = new InnerContextResource();
}

private static final class InnerContextResource {

private ApplicationContext context;

private void setContext(ApplicationContext context) {
this.context = context;
}
}

public static ApplicationContext getApplicationContext() {
return AplicationContextHolder.CONTEXT_PROV.context;
}

@Override
public void setApplicationContext(ApplicationContext ac) {
AplicationContextHolder.CONTEXT_PROV.setContext(ac);
}

}
@Data
@Getter
@Setter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class BaseEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", updatable = false, nullable = false)
protected Long id;

@CreatedDate
private Date createdAt;

@CreatedBy
private Long createdBy;

@LastModifiedDate
private Date updatedAt;

@LastModifiedBy
private Long updatedBy;

@PrePersist
protected void prePersist() {
if (this.createdAt == null) createdAt = new Date();
if (this.updatedAt == null) updatedAt = new Date();
}

@PreUpdate
protected void preUpdate() {
this.updatedAt = new Date();
}

@PreRemove
protected void preRemove() {
this.updatedAt = new Date();
}

}

最佳答案

你有一个StackOverflowError,它强烈表明你在某处有一些无限递归(或者至少是一个非常深的递归):

Caused by: java.lang.RuntimeException: java.lang.StackOverflowError

com.mycompany.test.config.AuditorAwareImpl.getCurrentAuditor 在您很长的堆栈跟踪中重复出现的事实表明它以某种方式参与了您的无限递归,我敢打赌它来自此类,以某种方式触发首先触发它的任何内容,可能是org.springframework.data.auditing.AuditingHandler。因此,请检查您的 AuditorAwareImpl 代码和/或审核配置。

关于java - 为什么无法使用 JPA 更改实体的引用对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57969538/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com