不想坚持一对一的实体 [英] Don't want to persist one-to-one entity
问题描述
假设我有两个类Employee
和Department
.
在Employee
中我写了:
@OneToOne( fetch = FetchType.EAGER, cascade = {CascadeType.ALL})
@JoinColumn(name="deptID")
private Department department;
和Department
类具有ID和Name. (就像系主任一样.)
And Department
class having Id and Name. (Just like Master of department).
我有一个视图,可以在其中使用选择/组合框选择Department
.当我提交表单并传递Employee
对象以持久化时,它给出了detached entity passed to persist
.我只想保存具有外键值的员工实体,而Department
已经有记录.
I have a view where I am selecting Department
using select/combo box. When I submit form and pass Employee
object to persist it gives detached entity passed to persist
. I want to save only employee entity with foreign key value, whereas Department
is already having records.
推荐答案
您已将JPA级联所有!并通过分离对象进行保存,这是一个问题.根据休眠文档一个>
CascadeType.PERSIST
在您保留员工时保留部门时起作用.
根据休眠文档:
CascadeType.PERSIST: cascades the persist (create) operation to associated entities persist() is called or if the entity is managed
因此,您的选择:
You told JPA cascade all! and passed detached object to save, that is an issue. According to Hibernate doc
CascadeType.PERSIST
works when you persist department when you persists employee.
As per hibernate doc:
CascadeType.PERSIST: cascades the persist (create) operation to associated entities persist() is called or if the entity is managed
So your options:
- 通过管理或临时部门对象附加到员工 对象.
- 合并分离的对象后再坚持吗?
- 删除全部层叠并仅选择所需的层叠类型
- 完全删除级联类型.
- Pass managed or transient department object attached to employee object.
- Merge detached one before persisting?
- Remove cascade all and select only cascade types you need
- Remove cascade type at all.
请参阅休眠文档
这篇关于不想坚持一对一的实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!