具有多个相同类映射的Parent-Child的JPA映射

时间:2017-10-12 07:28:46

标签: java hibernate jpa

我有下表

Task
---------------------
id
name

Action
--------------------
id
name 
task_fk
target_id

我做了以下JPA映射

class Task{
Long id;

@OneToMany(mappedBy = "task", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Action> actions = new ArrayList<>();
// other code
}

class Action{
Long id;

@ManyToOne
@JoinColumn(name = "target_id")
Task targetTask;

@ManyToOne
@JoinColumn(name = "task_fk", nullable = false)
Task task;

}

当我尝试保存任务时,对于具有目标任务设置为持久性错误

的操作的任务失败
 if (entityInformation.isNew(task)) {
                entityManager.persist(task);
 } else {
                entityManager.merge(task);
}

Error:org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.TransientPropertyValueException: object references an unsaved transient instance - save the transient instance before flushing : 

1 个答案:

答案 0 :(得分:0)

您可以使用尚未保留/存在的值设置var sel = $("select"); // if you have 1 select, if more - use selector by id/class/attr var options = ""; $.each(product, function(idx, val){ options = options + '<option value="' + idx + '">' + val + '</option>'; }) $(sel).html(options); 或其他字段。 如您的例外中所述:

  

对象引用未保存的瞬态实例