Are JPA Ids sequential

自作多情 提交于 2019-12-10 11:24:11

问题


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

}

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.


回答1:


The default strategy() for @GeneratedValue is GenerationType.AUTO. The definition is

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.

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.



来源:https://stackoverflow.com/questions/9771532/are-jpa-ids-sequential

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!