Home > OS >  Why JPA update entity causes stackoverflow?
Why JPA update entity causes stackoverflow?

Time:12-18

I have an entity Task with id. Tasks belongs to Config. I need to update Task with it's Config doesn't change. Here is my code: Task:


@Entity
@Getter
@Setter
public class Task{

    @ManyToOne
    @JoinColumn(name = "config_id", referencedColumnName = "id")
    private Config config;
    @OneToMany(orphanRemoval = true,cascade = CascadeType.ALL)
    @JoinColumn(name="task_id")
    private Set<ActivityItemTask> activityItemTasks = new HashSet<>();

}

@Entity
public class ActivityItemTask {

    private Double score;
    @EmbeddedId
    private ActivityItemTaskId activityItemTaskId;
    @Getter
    @Setter
    @Embeddable
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ActivityItemTaskId implements Serializable {
        @ManyToOne
        @JoinColumn(name = "activity_item_id")
        private ActivityItem activityItem;

        @ManyToOne
        @JoinColumn(name = "task_id")
        private Task task;

        @ManyToOne
        @JoinColumn(name = "config_id")
        private TaskConfig config;

    }
}

Config:

@Entity
@Getter
@Setter
public class Config{
    @OneToMany(mappedBy = "config")
    private Set<Task> tasks = new HashSet<>();
}

TaskService:

@Service
public class TaskService{
  @Resource
  TaskRepository taskRepository;

 @Transactional
 public Long save(Taskdto dto){
        Config config = new Config();
        config.setId(task.getConfigId());
        s.setTaskConfig(config);
        return taskRepository.save(s).getId();
 }
}

TaskDto:

@Data
public class TaskDto {
    private Long id;
    @NotNull
    private Long configId;
    private String name;
    private Date beginDate;
    private Date endDate;
    private String note;
}

when TaskService#save was called , it throw StackOverflowException:

org.springframework.web.util.NestedServletException: Handler dispatch failed; nested exception is java.lang.StackOverflowError

the log shows that my application querys task record and querys task's config and config's tasks and so on.

I am wondering what's wrong with my association annation. Any advice are appreciated.


I'm sorry.I have written another 2 calss so that I can find out the truth. It turns out my third calss ActivityItemTask may be the root cause. I think Task. activityItemTasks may should be annnotation with mappedBy=? But which field should be writtern here?

CodePudding user response:

You have a bi-directional dependency and as the error comes from Spring web I assume that the data should be serialized to JSON.

So you have to tell Jackson which dependency should be used to serialize.

@JsonReference and @JsonBackReference should be used.

Please find out more https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion

CodePudding user response:

I did config wrong association. Task should be :

    @OneToMany(mappedBy = "activityItemTaskId.task")
//    @JoinColumn(name = "task_id")
    private Set<ActivityItemTask> activityItemTasks = new HashSet<>();

This is how to use mappedBy annotation with embedable class.

Thank all you guys commented or answered. You did help me find it out.

  •  Tags:  
  • jpa
  • Related