简体   繁体   English

为什么 hibernate 为 OneToMany 映射生成插入和更新

[英]Why hibernate generates insert and update for OneToMany mapping

I am trying to understand the one-to-many mapping in Hibernate with a small example.我试图通过一个小例子来理解 Hibernate 中的一对多映射。 I have a Product with a set of Part's .我有一个Product使用一组Part's Here are my entity classes:这是我的实体类:

Part.java

@Entity
public class Part {
    @Id
    @GeneratedValue
    int id;
    String partName;
    //Setters & Getters
}

Product.java

@Entity
public class Product {
    private String serialNumber;
    private Set<Part> parts = new HashSet<Part>();
    @Id
    public String getSerialNumber() {
        return serialNumber;
    }
    @OneToMany
    @JoinColumn(name = "PRODUCT_ID")
    public Set<Part> getParts() {
        return parts;
    }
    // Setter methods
}

Then I tried to save some parts and products in my database and observed below queries generated by hibernate:然后我尝试在我的数据库中保存一些零件和产品,并观察到以下由 hibernate 生成的查询:

Hibernate: insert into Product (serialNumber) values (?)
Hibernate: insert into Part (partName, id) values (?, ?)
Hibernate: update Part set PRODUCT_ID=? where id=?

Here to add a record in Part table, hibernate generates 2 DML operations - insert and update .这里要在Part表中添加一条记录,hibernate 会生成 2 个 DML 操作 - insertupdate If a single insert command is sufficient to add a record in table then why hibernate uses both insert and update in this case?如果单个insert命令足以在表中添加记录,那么为什么在这种情况下休眠同时使用插入和更新? Please explain.请解释。

I know this is crazy old but I had the same problem and Google brought me here, so after fixing it I figured I should post an answer.我知道这太老了,但我遇到了同样的问题,谷歌把我带到了这里,所以在修复它之后我想我应该发布一个答案。

Hibernate will switch the insert/update approach to straight inserts if you make the join column not nullable and not updatable, which I assume in your case it is neither anyways:如果您使连接列不可为空且不可更新,则 Hibernate 会将插入/更新方法切换为直接插入,我认为在您的情况下无论如何都不是:

@JoinColumn(name = "PRODUCT_ID", nullable = false, updatable = false)

If Part as composite element list then only two query will come.如果 Part 作为复合元素列表,那么只会出现两个查询。 Please check and revert.请检查并恢复。

If its not a composite element , hibernate try to insert individual as a separate query and it will try to create relationship between them.如果它不是复合元素,hibernate 会尝试将个体作为单独的查询插入,并尝试在它们之间创建关系。

In earlier case hibernate will insert with relationship key.在较早的情况下,休眠将插入关系键。

**Hibernate: insert into Product (serialNumber) values (?) **休眠:插入产品 (serialNumber) 值 (?)

Hibernate: insert into Part (partName, id) values (?, ?)** Hibernate:插入 Part (partName, id) 值 (?, ?)**

In these two queries hibernate is simply inserting a record into the database.在这两个查询中,hibernate 只是将一条记录插入到数据库中。 At that stage hibernate is not creating any relationship between the two entities.在那个阶段,hibernate 不会在两个实体之间创建任何关系。

Hibernate: update Part set PRODUCT_ID=?休眠:更新部件集 PRODUCT_ID=? where id=?哪里 id=?

Now after making entity tables,hibernate is going to make a relationship between the two by using the above third query...现在在制作实体表后,hibernate 将使用上面的第三个查询在两者之间建立关系......

The association is uni-directional, so Product is the owning side (because it's the only side).关联是单向的,因此Product是拥有方(因为它是唯一的一方)。

Make the association bidirectional and make Part the association owner.使关联双向并使Part成为关联所有者。 That way you will avoid redundant updates because the foreign key values will be specified as part of insert statements for Part .这样您将避免冗余更新,因为外键值将被指定为 Part 的插入语句的Part

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

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