- Developing Middleware in Java EE 8
- Abdalla Mahmoud
- 195字
- 2021-07-23 19:24:37
Primary keys
A primary key field is mapped by using the @Id annotation as in the following example:
@Entity public class Movie { @Id private long id; }
Note that the primary key field itself should not be named Id; this is just a very common situation where we use a field with the name id as the primary key of our table. However, the primary key field can be named anything.
You can also use the primary key field on a getter method in the following example:
@Entity public class Movie { private long id; @Id public long getId() { return id; } }
The difference between the two approaches is that if you used the @Id annotation on a class field directly, the persistence provider will read all other mapping annotations from class fields. Otherwise, it will read all other mappings from the getter methods. You cannot mix annotations on both fields and getters, and the @Id annotation is a descriminator here. The choice between either approaches is the developer's preference. I personally prefer using field mapping in order to make all mapping information available at the beginning of the class.
推薦閱讀
- Instant Node Package Manager
- Spring 5.0 Microservices(Second Edition)
- MySQL數據庫管理實戰
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- CMDB分步構建指南
- Instant QlikView 11 Application Development
- 用Flutter極速構建原生應用
- HTML5從入門到精通 (第2版)
- C和C++游戲趣味編程
- Vue.js 2 Web Development Projects
- Python程序設計:基礎與實踐
- 生成藝術:Processing視覺創意入門
- JavaScript程序設計實例教程(第2版)
- 流程讓管理更高效:流程管理全套方案制作、設計與優化
- React Router Quick Start Guide