- 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.
推薦閱讀
- Oracle從入門到精通(第3版)
- 現代C++編程:從入門到實踐
- 國際大學生程序設計競賽中山大學內部選拔真題解(二)
- Learning PostgreSQL
- C++ Builder 6.0下OpenGL編程技術
- 看透JavaScript:原理、方法與實踐
- 你不知道的JavaScript(中卷)
- Ext JS 4 Web Application Development Cookbook
- 大話Java:程序設計從入門到精通
- 深入淺出Go語言編程
- LabVIEW數據采集(第2版)
- jQuery Essentials
- jBPM6 Developer Guide
- HTML5+jQuery Mobile移動應用開發
- Scala實用指南