Skip to content

Instantly share code, notes, and snippets.

@gaol
Created March 14, 2013 06:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gaol/5159223 to your computer and use it in GitHub Desktop.
Save gaol/5159223 to your computer and use it in GitHub Desktop.
How to persist one entity once, and refer it after that during one persistence call ?
Here is the situation:
Entity A:
-- List<Entity B>
Entity B:
-- List<Entity C>
Entity C:
Long id;
String type;
String key;
One Entity A has Multiple Entity B.
One Entity B has Multiple Entity C.
Entity C has unique constraint of (type + key)
The data of the Entities are manually inputted by user in a web page.
我要 persist 一个 Entity A 实例, 用户手动输入了 2 个 Entity B 实例,
而每个 Entity B 实例里都有一个 Entity C 实例。 那么用户输入完之后, 得到的数据模型就是:
A1
-- B1
-- C1
-- B2
-- C2
这个时候 EntityManager.persist(A1); 就会创建 2 个 Entity C 的实例: C1, C2.
The question is:
如果用户实际上想关联同一个 Entity C 的实例到 B1 和 B2 上,
也就是 C1 === C2(比如输入的 type 和 key 是一样的),
那么怎么让 EntityManager.persist(A1) 方法只创建一个 Entity C 的实例呢?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment