原创

休眠|保存实体时执行两个查询(插入和更新)

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

I have one question regarding saving entities with one ArrayList of objects as property.

This is the case. I have the following entity:

@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;

    @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);
    }
}

Here is the DescriptionContent class:

@Value
@Builder
public class DescriptionContent {

    String value;

    String name;
}

When I want to save TermEntryEntity with one DescriptionContent then two queries 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 11:39:45,176 TRACE  .descriptor.JdbcBindingLogging: binding parameter [1] as [VARCHAR] - [modification_user]
2024-04-11 11:39:45,177 TRACE  .descriptor.JdbcBindingLogging: binding parameter [2] as [BIGINT] - [1712828383395]
2024-04-11 11:39:45,177 TRACE  .descriptor.JdbcBindingLogging: binding parameter [3] as [VARCHAR] - [[{"value":"descValue","name":"descName"}]]
2024-04-11 11:39:45,177 TRACE  .descriptor.JdbcBindingLogging: binding parameter [4] as [BIGINT] - [1]
2024-04-11 11:39:45,177 TRACE  .descriptor.JdbcBindingLogging: binding parameter [5] as [BIGINT] - [1]
Hibernate: update term_entry set modified_by=?,modified_on=?,term_entry_description=?,termbase_id=? where term_entry_id=?
2024-04-11 11:39:45,503 TRACE  .descriptor.JdbcBindingLogging: binding parameter [1] as [VARCHAR] - [modification_user]
2024-04-11 11:39:45,504 TRACE  .descriptor.JdbcBindingLogging: binding parameter [2] as [BIGINT] - [1712828383395]
2024-04-11 11:39:45,504 TRACE  .descriptor.JdbcBindingLogging: binding parameter [3] as [VARCHAR] - [[{"value":"descValue","name":"descName"}]]
2024-04-11 11:39:45,504 TRACE  .descriptor.JdbcBindingLogging: binding parameter [4] as [BIGINT] - [1]
2024-04-11 11:39:45,504 TRACE  .descriptor.JdbcBindingLogging: binding parameter [5] as [BIGINT] - [1]

Does anybody know the reason why is that?

Note: This isn't reproducible when I want to add TermEntryEntity without Description.

正文到此结束
热门推荐
本文目录