JPA ID是连续的吗 [英] Are JPA Ids sequential

查看:139
本文介绍了JPA ID是连续的吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

播放框架会为实体创建一个ID,从而将模型类扩展如下:

Play framework creates an Id for Entities which extend the model class as such:

@MappedSuperclass
public class Model extends GenericModel {

    @Id
    @GeneratedValue
    public Long id;

    public Long getId() {
        return id;
    }

    @Override
    public Object _key() {
        return getId();
    }

}

是否可以使用此ID(最大ID)来确定最近创建的记录?

Is it possible to use this id (max id) to determine the latest created record?

在发生故障的情况下,我想使某些作业从某个角度重新运行,这是迄今为止添加此功能的最简单方法.

I want to make some jobs rerunnable from a certain point in the event of failures and this would be by far the easiest way to add this functionality.

我不想添加创建时间列,因为表已经很大.

I do not want to add a creation time column as the table is already huge.

推荐答案

@GeneratedValue的默认strategy()GenerationType.AUTO.定义是

指示持久性提供程序应为特定数据库选择适当的策略. AUTO生成策略可能期望数据库资源存在,或者可能尝试创建一个数据库资源.如果供应商不支持架构生成或无法在运行时创建架构资源,则供应商可能会提供有关如何创建此类资源的文档.

Indicates that the persistence provider should pick an appropriate strategy for the particular database. The AUTO generation strategy may expect a database resource to exist, or it may attempt to create one. A vendor may provide documentation on how to create such resources in the event that it does not support schema generation or cannot create the schema resource at runtime.

因此,这取决于您使用的数据库.如果使用GenerationType.IDENTITY,则某些数据库供应商将使用某种"auto_increment"值.至少对于MySQL来说是这样.如果将MySQL与GenerationType.IDENTITY结合使用,则可以使用max(id)确定最新创建的记录.有关更多信息,我将检查您的数据库规格.

So it depends on the database you use. If you use GenerationType.IDENTITY, some database vendors use some kind of "auto_increment" value. At least, this is true for MySQL. If you use MySQL with GenerationType.IDENTITY you can use max(id) to determine the latest created record. For further information, I'd check your database spec.

这篇关于JPA ID是连续的吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆