以下是实体父子关系
@MappedSuperclass
public class ConcurrencySafeEntity {
@Id
String id;
}
父实体
@Entity
@Access(AccessType.FIELD)
public class Label extends ConcurrencySafeEntity {
//Child
@OneToMany(orphanRemoval = true, mappedBy = "label")
@Cascade(CascadeType.SAVE_UPDATE)
private Set<Forecasted> shipments;
}
子实体层次结构
继承超级
@Access(AccessType.FIELD)
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@MappedSuperclass
public abstract class Super extends ConcurrencySafeEntity implements Comparable<Super> {
...
}
儿童班
@Entity
@Access(AccessType.FIELD)
public class Forecasted extends Super {
@ManyToOne(optional = false)
@JoinColumn(name = "forecast_label")
private Label label;
}
Spring Data Repository
public interface ForecastLabelRepository extends JpaRepository<Label, String>,
GenericRepository<Label, String> {
}
@Repository
public interface GenericRepository<T, ID> {
<S extends T> S save(S var1);
<S extends T> Iterable<S> save(Iterable<S> var1);
T findOne(ID var1);
}
使用以下服务方法
引导以上所有代码@Transactional(timeout = 60)
public void run(Label label) {
Set<Forecasted> shipments = service
.forecast(
label
);
label.addForecasts(shipments);
labelRepository.save(label);
}
现在当我执行时我得到以下错误,它无法保存标签,因为它无法找到预测,我认为应该自动级联,尝试持续预测,但显然无法找到标签,下面是堆栈跟踪代码段
Exception in thread "main" org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find xxx.xxxxxxx.xxxxxxx.xxxxx.models.forcasting.management.Forecasted with id 09f7f2ae-4ffe-41e4-bdaf-4d833d1467d8; nested exception is javax.persistence.EntityNotFoundException: Unable to find xxx.xxxxxxx.xxxxxxx.xxxxx.models.forcasting.management.Forecasted with id 09f7f2ae-4ffe-41e4-bdaf-4d833d1467d8
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:389)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:246)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:491)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213)
at com.sun.proxy.$Proxy100.save(Unknown Source)