我的代码: 我无法提交JPA交易;嵌套的异常是javax.persistence.RollbackException:提交事务时出错。
<spring-boot.version>2.0.3.RELEASE</spring-boot.version>
当classeB具有classeC的集合时发生错误
@Service
public class ServiceImpl implements Service{
private Repository repository;
@Autowired
public ServiceImpl(Repository repository) {
this.repository = repository;
}
@Transactional
public CLASSEA add(Long id, CLASSEB classeB) {
Optional<CLASSEA > optionalClasseA = repository.findById(id);
if (optionalClasseA.isPresent()) {
CLASSEA classA= optionalClasseA .get();
classA.addClasseB(classeB);
return repository.save(classA);
}
return null;
}
}
休眠实体:
@Entity
public class CLASSEA implements Serializable {
@OneToMany(mappedBy = "classeA", cascade = CascadeType.ALL)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<CLASSEB> classB= new HashSet<>();
}
public class CLASSEB implements Serializable {
@OneToMany(mappedBy = "classeB", cascade = CascadeType.MERGE)
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<CLASSEC> classeC= new HashSet<>();
@ManyToOne
@JsonIgnoreProperties("paymentItems")
private ClasseA classeA;
}
@Entity
public class CLASSEC implements Serializable {
@ManyToOne
private CLASSEB classeB;
}
POST请求:
classe B : {
"amount": 7845,
"classeC": [ { ..
}],
"classeA": null,
"day": "2018-07-19T10:46:04.245+02:00",
"id": null
}