简体   繁体   English

使用JPA删除/删除实体-保留在数据库中

[英]Delete/Remove entities with JPA- remain in database

My JPA entity: 我的JPA实体:

@Entity
public class Test implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @Lob
    public byte[] data;

}

Now, let's say I store 100 entries in my database and each entry contains 3 MB. 现在,假设我在数据库中存储了100个条目,每个条目包含3 MB。

SELECT x FROM Test x returns 100 entries and the database (on the file system) has a size of about 300 MB (as expected). SELECT x FROM Test x返回100个条目,并且数据库(在文件系统上)具有大约300 MB的大小(按预期)。

The next step is deleting all 100 entries by calling: entityManager.remove(test) for each entry. 下一步是通过调用每个条目的entityManager.remove(test)来删除所有100个条目。

SELECT x FROM Test x now results in an empty table, BUT the database still has got a size of 300 MB! SELECT x FROM Test x现在导致一个空表,但是数据库仍然有300 MB的大小! First if I drop the table, the database will shrink to the initial state. 首先,如果我删除表,数据库将缩小到初始状态。

What's going wrong here? 这是怎么了 If I delete entries, they won't really get removed?! 如果我删除条目,它们真的不会被删除吗?

I tried with JavaDB and Oracle XE and I'am using EclipseLink. 我尝试使用JavaDB和Oracle XE,并且正在使用EclipseLink。

For me I will see whether the JPA transaction was committed successfully and select entity counts from database console to double check. 对我来说,我将查看JPA事务是否已成功提交,并从数据库控制台中选择实体计数以再次检查。

If table records not really gone it means you might have some trouble deleting the records. 如果表记录没有真正消失,则意味着删除记录可能会遇到一些麻烦。 Try commit trasnaction or flush it. 尝试提交事务或刷新它。

If the table records already gone but database disk space remains occurred, means it might be becoz of the database space associate management policies, then check your database configuration see how to get the space released once records are gone. 如果表记录已经消失但数据库磁盘空间仍然存在,则意味着它可能是数据库空间关联管理策略的原因,然后检查数据库配置以查看记录消失后如何释放空间。

The database does not delete the data physically (obviously). 数据库不会物理删除数据(显然)。 When and how this is done depends on the database and its setup, eg it could be triggered by certain file size thresholds, manual compact commands or scheduled maintance tasks. 何时以及如何完成取决于数据库及其设置,例如,可能由某些文件大小阈值,手动紧凑命令或计划的维护任务触发。 This is completely independent of JPA. 这完全独立于JPA。

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

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