使用调度程序线程在 spring jpa 中传递到持久化的分离实体错误

问题描述 投票:0回答:1

当我尝试保存并刷新 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)

此行为的解决方案和解释?

java spring-mvc spring-data-jpa schedule java-threads
1个回答
0
投票

你可以检查一下吗https://docs.spring.io/spring-data/jpa/reference/jpa/transactions.html也许可以帮助!

© www.soinside.com 2019 - 2024. All rights reserved.