- 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.
推薦閱讀
- VMware View Security Essentials
- Bootstrap Site Blueprints Volume II
- Visual C++程序設計教程
- Java系統分析與架構設計
- PaaS程序設計
- 實戰Java程序設計
- Python爬蟲開發與項目實戰
- Eclipse Plug-in Development:Beginner's Guide(Second Edition)
- KnockoutJS Starter
- Mastering Linux Network Administration
- Java EE核心技術與應用
- Tableau 10 Bootcamp
- Go語言精進之路:從新手到高手的編程思想、方法和技巧(2)
- 數字媒體技術概論
- Groovy 2 Cookbook