【问题标题】:Does a CrudRepository delete also remove underlying large objects of Blob fieldsCrudRepository 删除是否也会删除 Blob 字段的底层大对象
【发布时间】:2019-12-01 18:32:30
【问题描述】:

我有一个带有@Lob 注释的Blob 字段的实体:

@Entity
public class Entry {

    @Id
    @GeneratedValue
    private Long tid;

    @Lob
    private Blob content;
}

content 是使用 Hibernate 的 LobCreator 创建的,然后使用 JPA CrudRepository 保存实体:

public Entry save(Entry template, InputStream source) {
    Session session = entityManager.unwrap(Session.class);
    content = Hibernate.getLobCreator(session).createBlob(source);
    template.setContent(content);
    return entryRepository.save(template);
}

当使用 PostgreSQL 作为底层数据库时,给定 Entrycontent 列将不包含实际的大对象本身,而是一个引用它的 oid

假设我现在使用CrudRepositorydelete 给定实体我想知道我是否可以确定不仅行本身将被删除,而且大对象也会被删除,或者我是否会留下一个孤儿大物体。

我可以使用PostgresSQL specific functionality 解决这个问题,但如果可能的话,我想坚持使用与数据库无关的 Hibernate 方法。

【问题讨论】:

  • blob 在逻辑上是该行的一部分吗?

标签: java postgresql hibernate spring-data-jpa


【解决方案1】:
public class FileData {

    @Lob
    @Column(name = "file_data")
    private Blob fileDataBlob;

    public void freeFileDataBlob() throws SQLException {
        if (this.fileDataBlob != null){
            this.fileDataBlob.truncate(0L);
            this.fileDataBlob.free();
            this.fileDataBlob = null;
            LOG.info("freeFileDataBlob() successful");
        }
    }

}

【讨论】:

    猜你喜欢
    • 2018-11-21
    • 1970-01-01
    • 2013-02-11
    • 2017-01-20
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 2021-10-16
    • 2020-01-05
    相关资源
    最近更新 更多