不想坚持一对一的实体 [英] Don't want to persist one-to-one entity

查看:40
本文介绍了不想坚持一对一的实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个类EmployeeDepartment.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆