What is the purpose of the following annotation in JPA? @Id @GeneratedValue(strategy = GenerationType.IDENTITY)

4 phương ánCó giải thích
Đề bài
private Long id;

Các phương án trả lời

Chọn một phương án
Lời giảiChọn phương án để mở
Đáp án đúng: "It marks the field as the primary key with auto-increment strategy". Lần theo mã: @Id đánh dấu trường id là khoá chính của entity, còn @GeneratedValue(strategy = GenerationType.IDENTITY) báo JPA không tự cấp giá trị mà để cột identity hay auto-increment của cơ sở dữ liệu sinh khi INSERT, sau đó provider đọc lại khoá vừa sinh và gán vào trường id kiểu Long. "It creates a foreign key for the entity" sai vì khoá ngoại khai báo bằng @ManyToOne hoặc @OneToOne kèm @JoinColumn, không phải @Id. "It marks the field as a version column" sai vì cột phiên bản cho khoá lạc quan (optimistic locking) dùng @Version. "It disables the persistence of the field" sai vì muốn một trường không được lưu xuống bảng thì dùng @Transient; ở đây trường id lại là trường quan trọng nhất được lưu.

Cùng phần 1

Xuất hiện trong 1 bộ đề