英文:
Which side of bidirectional mapping should be persisted
问题
I was looking for an answer on SO
and several articles, but I think clear explanation is unfortunately missed, so I decided to ask.
Say we have two entities (box contains several items):
@Entity
@Table(name = "box")
class Box
{
@Id
private Long id;
@OneToMany(mappedBy = "box")
private List<Item> items = new LinkedList<>();
public void addItem(Item item)
{
items.add(item);
item.setBox(this);
}
// setters, getters, delete synchronize method
}
@Entity
@Table(name = "item")
class Item
{
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "box_id")
private Box box;
// setters getters
}
Which side of the relationship should I persist?
- Does it depend on the owning side of the relationship?
- Does it depend on the cascade type and owning side has nothing in common?
- Place CascadeType.PERSIST in the Box entity, then em.persist(box)
- Place CascadeType.PERSIST in the Item entity, then em.persist(item)
- Both, and persist is up to me - can be em.persist(box) or em.persist(item)
英文:
I was looking for an answer on SO
and several articles, but I think clear explanation is unfortunately missed, so I decided to ask.
Say we have two entities (box contains several items):
@Entity
@Table(name = "box")
class Box
{
@Id
private Long id;
@OneToMany(mappedBy = "box")
private List<Item> items = new LinkedList<>();
public void addItem(Item item)
{
items.add(item);
item.setBox(this);
}
// setters, getters, delete synchronize method
}
@Entity
@Table(name = "item")
class Item
{
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "box_id")
private Box box;
// setters getters
}
Which side of the relationship should I persist?
- Does it depend on the owning side of the relationship?
- Does it depend on the cascade type and owning side has nothing in common?
- Place
CascadeType.PERSIST
in theBox
entity, thenem.persist(box)
- Place
CascadeType.PERSIST
in theItem
entity, thenem.persist(item)
- Both, and
persist
is up to me - can beem.persist(box)
orem.persist(item)
- Place
答案1
得分: 1
你可以从两个方面持久化关系。
如果你持久化盒子实体,你只需要调用一次实体管理器的持久化方法,并将盒子实体作为参数传递,其中包括项目实体列表,盒子项目参数必须具有级联类型。
如果你持久化项目实体,你将需要调用实体管理器的持久化方法N次,其中N是项目数量,传递项目实体和盒子参数,盒子参数必须具有级联类型。在第一次插入时,Box将没有ID,但一旦插入第一个Item,Box将有ID,因此在接下来的N-1个Item中,你必须使用具有ID的Box,否则它将创建N个Box。
基于所有这些,我认为在大多数逻辑中,更好的做法是持久化父实体,并在表示与子类的关系的属性上使用级联类型的持久化。
英文:
You can persist the relationship from both sides.
If you persist the box entity, you will only have to call the persist method of the entity manager once and pass the box entity as a parameter, with the item entity list, the box items parameter will have to have a cascade type.
If you persist the item entity, you will have to call the persist method of the entity manager N times, being N the number of items, passing the Item entity with the box parameter, the box parameter will have to have a cascade type. In the first insertion Box will not have ID, but once you insert the first Item, Box will have ID so in the following N-1 Items you will have to establish that Box with Id since if not, it will create N boxes.
Based on all this I think that in most logics it is better to persist the parent entity, cob cascade type persist in the property that represents the relation with the daughter class.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论