- EJB 3 Developer Guide
- Michael Sikora
- 303字
- 2021-07-02 11:34:55
Field-Based Annotations
Recall we used a property-based annotation for the primary key @Id
in the Customer entity. This results in the persistence engine using getter and setter methods to access and set the entity state. In this section we will modify the Customer entity to use field-based, rather than property-based annotations. The following listing demonstrates this:
@Entity public class Customer implements java.io.Serializable { @Id private int id; private String firstName; @Basic private String lastName; public Customer() {}; public Customer(int id, String firstName, String lastName){ this.id = id; this.firstName = firstName; this.lastName = lastName; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstname() { return firstName; } public void setFirstname(String firstName) { this.firstName = firstName; } public String toString() { return "[Customer Id =" + id + ",first name=" + firstName + ",last name=" + lastName + "]"; } }
Note that we have placed the primary key @Id
annotation immediately before the field declaration. If we use a field-based annotation then all other annotations, other than class level annotations such as @Entity
, must also be field-based annotations. In the case of field-based annotations the persistence engine will use reflection to access and set the entity state. Getter and setter methods may be present in the entity but they are ignored by the persistence engine. To demonstrate this point we have removed the getLastName()
and setLastName()
methods from the Customer entity.
We have seen that attributes other than the primary key are mapped to a database by default. We call these mappings basic mappings. A basic mapping is used where the attribute is a Java primitive type or any Serializable type such as a String
. We can explicitly flag these mappings using the @Basic
annotation. We have done this for the lastName
field.
- Photoshop CC 網店視覺設計
- Flash CC中文版動畫設計與制作/微課堂學電腦
- Instant Vert.x
- SPSS進階分析與實務
- Rhino 6.0中文版入門、精通與實戰
- Django 1.2 E/commerce
- 老郵差數碼照片處理技法 圖層篇
- Photoshop CS6中文版從入門到精通(核心技法卷):摳圖、修圖、Camera Raw、調色、銳化、合成
- Adobe創意大學Illustrator CS5 產品專家認證標準教材
- 老郵差·Photoshop數碼照片處理技法:人像篇(修訂版)
- SOA Patterns with BizTalk Server 2009
- RAW 數碼底片演義
- Instant Markdown
- 機械CAD軟件應用入門指導書
- Photoshop 2020實戰從入門到精通(超值版)