【问题标题】:Spring Data Repository: Detached entity passed to persistSpring Data Repository:传递给持久化的分离实体
【发布时间】:2014-12-17 17:53:06
【问题描述】:

配置文件对象有一个任务列表。 保存新配置文件时,任务列表也应与数据库同步(insertedupdated)。问题是配置文件存储库的 save() 方法仅允许一种或另一种方法取决于属性上方设置的级联属性(CascadeType.PERSIST 或 MERGE) .

个人资料类

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class AbstractProfile implements Profile {
    ...

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.PERSIST)
    private List<Task> tasks;

    ..

JUnit-Test 类

public class ProfileTaskRepositoryTest {

    @Autowired 
    private ProfileRepository profileRepo;    //extends JpaRepository
    @Autowired
    private TaskRepository taskRepo;          //extends JpaRepository

    @Test // ->this test passes
    public void testCreateProfileWithNewTask() {

        //profileData and taskData hold dummy data. They return new objects
        AbstractProfile interview = profileData.createITInterviewProfile();
        Task questionTask = taskData.createInterviewQuestionTask();

        //add new task to profile and save
        interview.addTask(questionTask);
        profileRepo.save(interview);

        //task repository confirms the insertion
        assertTrue(taskRepo.count() == 1); 
    }

    @Test // ->this test fails
    public void testCreateProfileWithExistingTask() {

        //first create task and save in DB
        Task questionTask = taskData.createInterviewQuestionTask();  // transient obj
        taskRepo.save(questionTask);  // questionTask becomes detached

        // then create a new profile and add the existing task.
        // the problem is the existing task is now detached)
        AbstractProfile interview = profileData.createITInterviewProfile();
        interview.addTask(questionTask);

        profileRepo.save(interview); // !!! this throws an exception
    }

我想 questionTask 对象在 taskRepo 将其保存在 DB 中然后关闭会话时会分离。

例外:

org.springframework.orm.jpa.JpaSystemException: org.hibernate.PersistentObjectException: detached entity passed to persist: *.Task; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: *.Task
...

profileRepo.save() 应该能够处理任务列表的insertionupdate。有没有优雅的方法来解决这个问题?

【问题讨论】:

  • Task 是什么?这是您设计的课程吗?你能发布那个代码吗?

标签: java spring hibernate jpa spring-data-jpa


【解决方案1】:

您应该将@Transactional 属性放在测试类上以避免异常。

@Transactional
@TransactionConfiguration
public class ProfileTaskRepositoryTest {
}

希望这会有所帮助。

【讨论】:

  • 测试类不需要这个。它应该已经添加到实际上扩展了 JpaRepository 的 TaskRepository 中。
  • 在问题中描述了 ProfileRepository 和 TaskRepository 从接口 JpaRepository 扩展。如果 CreateProfileWithExistingTask 是一个服务类方法,那么是的,@Transactional 属性应该在服务类上,除非您需要在事务中运行测试,否则无需将其放入测试类中。
【解决方案2】:

尝试级联 = { CascadeType.PERSIST, CascadeType.MERGE }

【讨论】:

  • 恐怕会抛出同样的异常。
【解决方案3】:

休眠会话已过期;

使用

spring.datasource.removeAbandoned=false

spring.datasource.removeAbandonedTimeout=...

【讨论】:

    猜你喜欢
    • 2020-11-02
    • 2017-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2017-04-01
    • 2018-09-22
    相关资源
    最近更新 更多