java JPA 在多对一关系中持久化对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4290520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPA persist object in a ManyToOne relationship
提问by tal
I have a company/employee @OneToMany
relation in my database defined as:
@OneToMany
我的数据库中有一个公司/员工关系,定义为:
@Entity
public class Employee {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
@ManyToOne @JoinColumn(name="companyid")
Company company;
....
}
@Entity
public class Company {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
...
}
Now I am adding a newly created employee to a detached company. The code I use is something like:
现在,我将新创建的员工添加到独立公司。我使用的代码是这样的:
Company company = em1.find(Company.class, 555L);
em1.close();
EntityTransaction et = em2.getTransaction();
et.begin();
Employee employee = new Employee();
employee.company = company;
em2.persist(employee);
et.close();
Will this work ok?
Is hibernate going to merge the company into the 2nd EntityManager or just use its id and persist the employee object?
Might hibernate somehow duplicate my company object or throw an exception saying that a company with the same id already exists in the DB?
这行得通吗?
hibernate 是将公司合并到第二个 EntityManager 还是只使用它的 id 并保留员工对象?
休眠可能会以某种方式复制我的公司对象或抛出异常,指出数据库中已存在具有相同 ID 的公司?
回答by axtavt
- In the described case
Company
'sid
will be used when persistingEmployee
object, butCompany
itself will not be merged (note thatEmployee
is the owning side of the relationship) - If
Company
is transient rather than detached, you will get "object references an unsaved transient instance" error - If
cascade = CascadeType.PERSIST
is used, you will get "detached entity passed to persist" error.
- 在描述的情况下
Company
'sid
将在持久化Employee
对象时使用,但Company
它本身不会被合并(注意Employee
是关系的拥有方) - 如果
Company
是瞬态而不是分离,您将收到“对象引用未保存的瞬态实例”错误 - 如果
cascade = CascadeType.PERSIST
使用,您将收到“将分离的实体传递给持久化”错误。
From JPA Specification:
从 JPA 规范:
If X is a managed entity, it is synchronized to the database.
- For all entities Y referenced by a relationship from X, if the relationship to Y has been annotated with the cascade element value cascade=PERSIST or cascade= ALL, the persist operation is applied to Y.
- For any entity Y referenced by a relationship from X, where the relationship to Y has not been annotated with the cascade element value cascade=PERSIST or cascade= ALL:
- If Y is new or removed, an IllegalStateException will be thrown by the flush operation (and the transaction marked for rollback) or the transaction commit will fail.
- If Y is detached, the semantics depend upon the ownership of the relationship. If X owns the relationship, any changes to the relationship are synchronized with the database;otherwise, if Y owns the relationships, the behavior is undefined.
如果 X 是受管实体,则将其同步到数据库。
- 对于由来自 X 的关系引用的所有实体 Y,如果已使用级联元素值级联元素值级联 = PERSIST 或级联 = ALL 注释了与 Y 的关系,则对 Y 应用持久操作。
- 对于由来自 X 的关系引用的任何实体 Y,其中与 Y 的关系尚未使用级联元素值级联 = PERSIST 或级联 = ALL 注释:
- 如果 Y 是新的或已删除,则刷新操作(以及标记为回滚的事务)将抛出 IllegalStateException 或事务提交将失败。
- 如果 Y 是分离的,则语义取决于关系的所有权。如果 X 拥有关系,则对关系的任何更改都会与数据库同步;否则,如果 Y 拥有关系,则行为未定义。