jpa Basic mapping Entity with sequence managed Id

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

Here we have a class and we want the identity field (userUid) to have its value generated via a SEQUENCE in the database. This SEQUENCE is assumed to be called USER_UID_SEQ, and can be created by a DBA, or can be created by the JPA provider.

@Entity
@Table(name="USER")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @SequenceGenerator(name="USER_UID_GENERATOR", sequenceName="USER_UID_SEQ")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="USER_UID_GENERATOR")
    private Long userUid;

    @Basic
    private String userName;
}


Got any jpa Question?