Hibernate - ManyToOne - 保存 - org.hibernate.TransientObjectException: [英] Hibernate - ManyToOne - Save - org.hibernate.TransientObjectException:
问题描述
当没有得到新的实体时,它抛出:org.hibernate.TransientObjectException:
我们使用的表结构:
DEPARTMENT(DEPARTMENT_ID BIGINT,NAME VARCHAR(128))
EMPLOYEE(EMPLOYEE_ID BIGINT,NAME VARCHAR(128),DEPARTMENT_ID BIGINT)
实体:
类别部门
{
@Id
Long departmentId;
字符串名称;
@版本
长版本号;
}
班员工
{
@Id
长员工ID;
字符串名称;
@ManyToOne
部门部门;
@版本
长版本号码
}
类具有所有字段和默认构造函数的构造函数和getter方法,这是构造函数主键作为参数)
现在,如果我想用departmentId保存Employee(例如100),是否需要首先获取Department记录,然后将其设置在员工中?
无法直接创建Department实例(通过设置主键(departmentId))并在Employee中设置Department实例并保存Employee?
当我这样做时,它会抛出org.hibernate.TransientObjectException。
有关此最佳实践的任何建议吗?
预先感谢您
谢谢ChssPly76和Wysawyg。
解决方案之一可能是:
我们将更新Employee POJO,如下所示:
ManyToOne(fetch = FetchType.EAGER)
@ ** JoinColumn **(name =DEPARTMENT_ID,referencedColumnName =DEPARTMENT_ID,** insertable = false,updatable = false **)
私人部门部门;
@Column(name =department_id)
private long departmentId;
(department和DepartmentId都有setter和getter方法)
和现在在这里(请参阅department和departmentId映射到同一列(DEPARTMENT_ID)
,我们只使用 department 来获取部门详细信息
和departmentId插入或更新员工
但我担心这是否是更好的方法。
While I try to save top level entity (using JPA), do I need to get the ManyToOne mapped entity freshly from database and set it or cannot I just set Id (of manyToOne mapped entity and save top level entity? When do not get fresh entity it throws: org.hibernate.TransientObjectException:
Table structures we are using:
DEPARTMENT(DEPARTMENT_ID BIGINT, NAME VARCHAR(128))
EMPLOYEE(EMPLOYEE_ID BIGINT, NAME VARCHAR(128), DEPARTMENT_ID BIGINT)
Entities:
class Department
{
@Id
Long departmentId;
String name;
@Version
Long versionNumber;
}
class Employee
{
@Id
Long employeeId;
String name;
@ManyToOne
Department department;
@Version
Long versionNumber
}
(both classes have setter and getter methods for all fields and default constructor, constructor which takes primary key as argument) Now if I want to save Employee with departmentId (say 100), do I need to get the Department record first and then set it in employee?
Cannot I create instance of Department directly (by setting primary key(departmentId)) and set Department instance in Employee and save Employee? When I do this it is throwing org.hibernate.TransientObjectException.
Any suggestions on best practice to be followed for this?
Thank you in advance
Thank you ChssPly76 and Wysawyg.
One of the solution could be: We will update Employee POJO as below
ManyToOne(fetch=FetchType.EAGER)
@**JoinColumn**(name = "DEPARTMENT_ID", referencedColumnName = "DEPARTMENT_ID", **insertable=false, updatable=false**)
private Department department;
@Column(name = "department_id")
private Long departmentId;
(both department and departmentId will have setter and getter methods)
and Now here (please see that both department and departmentId mapped to same column (DEPARTMENT_ID) we use department only for fetching Department details and departmentId to insert or update Employee
But I am worried if it is a better approach.
这篇关于Hibernate - ManyToOne - 保存 - org.hibernate.TransientObjectException:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!