简体   繁体   English

JPA在ManyToOne关系中持久化对象

[英]JPA persist object in a ManyToOne relationship

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? hibernate会将公司合并到第二个EntityManager中,还是仅使用其id并持久保存员工对象?
Might hibernate somehow duplicate my company object or throw an exception saying that a company with the same id already exists in the DB? 可能会以某种方式复制我的公司对象或者抛出异常,说明DB中已存在具有相同ID的公司?

  • In the described case Company 's id will be used when persisting Employee object, but Company itself will not be merged (note that Employee is the owning side of the relationship) 在描述的情况下, Companyid将在持久化Employee对象时使用,但Company本身不会被合并(请注意, Employee是关系的拥有方)
  • If Company is transient rather than detached, you will get "object references an unsaved transient instance" error 如果Company是暂时的而不是分离的,那么您将获得“对象引用未保存的瞬态实例”错误
  • If cascade = CascadeType.PERSIST is used, you will get "detached entity passed to persist" error. 如果使用cascade = CascadeType.PERSIST ,则会出现“已分离的实体传递给持久化”错误。

From JPA Specification: 来自JPA规范:

If X is a managed entity, it is synchronized to the database. 如果X是托管实体,则会将其同步到数据库。

  • 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. 对于来自X的关系引用的所有实体Y,如果与Y的关系已使用级联元素值cascade = PERSIST或cascade = ALL进行注释,则将持久化操作应用于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: 对于来自X的关系引用的任何实体Y,其中与Y的关系未使用级联元素值cascade = PERSIST或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. 如果Y是新的或被删除,则刷新操作(以及标记为回滚的事务)将抛出IllegalStateException,否则事务提交将失败。
    • If Y is detached, the semantics depend upon the ownership of the relationship. 如果分离Y,则语义取决于关系的所有权。 If X owns the relationship, any changes to the relationship are synchronized with the database; 如果X拥有该关系,则对该关系的任何更改都将与数据库同步; otherwise, if Y owns the relationships, the behavior is undefined. 否则,如果Y拥有关系,则行为未定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM