0

lets say i have Entity A and A Has List of B which is mark as OneToMany. do i have to fetch all of B items in order to add new B item to the list A has?

example:

@Entity
public class A {

   @Id
   Long id;

   @OneToMany
   List<B> bs;
} 

do i have to fetch all of A.bs in order to add new item to bs ?

thanks

Alon

EDIT few more questions:

@German: interesting! few questions

  1. what if the join between A and B is by Table C? do i get the same behavior?

  2. why does the list make troubles?

  3. what can i use instead of a list? Set?

  4. if I persist A do i overwrite all the links for B?

  5. what if the link is ManyToMany (using extra table)

4

2 回答 2

3

您必须将 b 添加到 A 的 bs 列表中。但是会发生什么是特定于提供者的。启用更改跟踪后,EclipseLink 等一些提供程序可以避免为延迟加载列表上的某些操作获取列表

于 2013-05-09T20:19:30.940 回答
1

如果 B 有一个指向 A 的 @ManyToOne 注释并且您不会使用该列表,您可能只需要设置 B 对 A 的引用并将其持久化,而根本不需要触摸 A 实例。在数据库中,数据将被插入到表 B 中,但在内存中,A 的列表将没有新元素。

于 2013-05-10T22:11:48.157 回答