emt14
emt14

Reputation: 4896

Are JPA Ids sequential

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.

Upvotes: 2

Views: 187

Answers (1)

kraftan
kraftan

Reputation: 6312

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.

Upvotes: 4

Related Questions