当我尝试保存并刷新 A 类对象时遇到此异常。(春季版本 2.7.8)
@Table(name= "table_a")
@Entity
@Data
public class A {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
@ManyToOne(optional = false, targetEntity = B.class, cascade = CascadeType.PERSIST)
private B b;
//...etc
}
和B类:
@Table(name= "table_b")
@Entity
@Data
public class B {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
//...etc
}
我的代码块:
public void saveData(A a){
aJpaRepository.saveAndFlush(a);
}
when saveData invoked from Api Call Request its work and cascaded correctly, but when I call it with same object from scheduler thread it occurred this exception:
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: com.example.model.B; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: com.example.model.B
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:297)
此行为的解决方案和解释?