原创

Hibernate @OneToMany 关系 |为什么在添加实体时执行更新查询?

温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

I have one question regarding the hibernate @OneToMany relationship. This is the case. I have the following @OneToMany relationship:

@Entity
@Table(name = "term_entry")
@NoArgsConstructor
@Getter
@Setter
@ToString
public class TermEntryEntity {

    @Id
    @Column(name = "term_entry_id", updatable = false, nullable = false)
    private Long termEntryId;

    @Column(name = "termbase_id", length = 10, nullable = false)
    private Long termbaseId;

    @Column(name = "modified_by", nullable = false)
    private String modifiedBy;

    @Column(name = "modified_on", nullable = false)
    private Long modifiedOn;

    @Column(name = "term_entry_description", columnDefinition = "longtext")
    @Convert(converter = DescriptionContentConverter.class)
    private List<DescriptionContent> termEntryDescriptions;

    @OneToMany(mappedBy = "termEntryEntity", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<TermEntity> terms = new ArrayList<>();

    public void addTerm(TermEntity termEntity) {
    terms.add(termEntity);
    termEntity.setTermEntryEntity(this);
    }

    @Override
    public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    TermEntryEntity that = (TermEntryEntity) o;
    return termEntryId.equals(that.termEntryId);
    }

    @Override
    public int hashCode() {
    return Objects.hash(termEntryId);
    }
}

@Entity
@Table(name = "term")
@NoArgsConstructor
@Getter
@Setter
public class TermEntity {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "term_entry_id")
    private TermEntryEntity termEntryEntity;

    @Id
    @Column(name = "term_id", updatable = false, nullable = false)
    private Long termId;

    @Column(name = "term_revision", columnDefinition = "longtext", nullable = false)
    @Convert(converter = TermContentConverter.class)
    private TermContent termContent;

    @Override
    public boolean equals(Object o) {
    if (this == o)
        return true;
    if (o == null || getClass() != o.getClass())
        return false;
    TermEntity that = (TermEntity) o;
    return termId.equals(that.termId);
    }

    @Override
    public int hashCode() {
    return Objects.hash(termId);
    }
}

When I want to add TermEntryEntity with two TermEntity using entityManager:

entityManager.persist(termEntryEntity);

then two queries for TermEntryEntity are executed (insert and update which are the same as we can see in the SQL logs)

    Hibernate: 
        insert 
        into
            term_entry
            (modified_by,modified_on,term_entry_description,termbase_id,term_entry_id) 
        values
            (?,?,?,?,?)
    2024-04-11 10:05:46,024 TRACE  .descriptor.JdbcBindingLogging: binding parameter [1] as [VARCHAR] - [TestUsername]
    2024-04-11 10:05:46,024 TRACE  .descriptor.JdbcBindingLogging: binding parameter [2] as [BIGINT] - [1712822745792]
    2024-04-11 10:05:46,024 TRACE  .descriptor.JdbcBindingLogging: binding parameter [3] as [VARCHAR] - [[{"value":"TEattributeValue","name":"AttributeName"},{"value":"attributeValue2","name":"AttributeName"},{"value":"attributeValue3","name":"AttributeName"}]]
    2024-04-11 10:05:46,024 TRACE  .descriptor.JdbcBindingLogging: binding parameter [4] as [BIGINT] - [1]
    2024-04-11 10:05:46,024 TRACE  .descriptor.JdbcBindingLogging: binding parameter [5] as [BIGINT] - [365891673479184384]
.
.
.
Two TermEtity insert queries.
.
.
.
      Hibernate: 
        update
            term_entry 
        set
            modified_by=?,
            modified_on=?,
            term_entry_description=?,
            termbase_id=? 
        where
            term_entry_id=?
    2024-04-11 10:05:46,030 TRACE  .descriptor.JdbcBindingLogging: binding parameter [1] as [VARCHAR] - [TestUsername]
    2024-04-11 10:05:46,030 TRACE  .descriptor.JdbcBindingLogging: binding parameter [2] as [BIGINT] - [1712822745792]
    2024-04-11 10:05:46,030 TRACE  .descriptor.JdbcBindingLogging: binding parameter [3] as [VARCHAR] - [[{"value":"TEattributeValue","name":"AttributeName"},{"value":"attributeValue2","name":"AttributeName"},{"value":"attributeValue3","name":"AttributeName"}]]
    2024-04-11 10:05:46,030 TRACE  .descriptor.JdbcBindingLogging: binding parameter [4] as [BIGINT] - [1]
    2024-04-11 10:05:46,030 TRACE  .descriptor.JdbcBindingLogging: binding parameter [5] as [BIGINT] - [365891673479184384]

Does someone know the reason why that is?

Note: TermEntites are inserted correctly ( two insert queries for two TermEntites ) This is my DB schema:

CREATE TABLE term_entry
(
    term_entry_id       bigint(20)       NOT NULL,
    termbase_id         bigint(20)       NOT NULL,
    term_entry_description      TEXT         DEFAULT NULL,
    modified_on bigint                   NOT NULL,
    modified_by varchar(255)             NOT NULL,
    PRIMARY KEY (term_entry_id),
    INDEX idx_term_entry_on_termbase_id (termbase_id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci;



CREATE TABLE term
(
    term_id bigint(20)                NOT NULL,
    term_entry_id bigint(20)          NOT NULL DEFAULT 0,
    term_revision    LONGTEXT         NOT NULL,
    PRIMARY KEY (term_id),
    INDEX idx_term_on_term_entry_id (term_entry_id)
) ENGINE = InnoDB
  DEFAULT CHARSET = utf8mb4
  COLLATE = utf8mb4_unicode_ci;
正文到此结束
热门推荐
本文目录