- EJB 3 Developer Guide
- Michael Sikora
- 344字
- 2021-07-02 11:34:55
Overriding Metadata Defaults
In this section we return to the Customer entity and override some of the default mapping options.
First we want to use the CLIENT
table with columns CUSTOMER_ID, FIRST_NAME
and LAST_NAME
to store Customer entities. The primary key is CUSTOMER_ID
. The table definition is:

The modified listing for Customer.java
is shown below:
@Entity
@Table(name = "CLIENT")
public class Customer implements java.io.Serializable {
private int id;
private String firstName;
private String lastName;
public Customer() {};
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "CUSTOMER_ID")
public int getId() { return id; }
public void setId(int id) { this.id = id; }
@Column(name = "FIRST_NAME")
public String getFirstname() { return firstName; }
public void setFirstname(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME")
public String getLastname() { return lastName; }
public void setLastname(String lastName) {
this.lastName = lastName;
}
public String toString() {
return "[Customer Id =" + id + ",first name=" +
firstName + ",last name=" + lastName + "]";
}
}
Note we have used the @Table
annotation to specify the table name as CLIENT
. The default table name is the entity name, CUSTOMER
in our example. The catalog
element specifies the database catalog where the table is located. Many database systems, Derby included, do not support the concept of a catalog. So in our example we can leave this element out. The schema
element specifies the database schema where the table is located. The default is persistence provider specific, in the case of the GlassFish APP
is the default schema for the embedded Derby database. Again, in our example, we rely on the default.
We have explicitly specified an AUTO
primary key generation strategy:
@Id @GeneratedValue(strategy=GenerationType.AUTO)
Recall that GlassFish selects the TABLE
strategy in this case.
Recall that by default column names are set to attribute names. To override this we use the @Column
annotation. For example,
@Column(name = "CUSTOMER_ID") public int getId() { return id; }
will map the id
attribute to the CUSTOMER_ID
column. In a similar fashion we map the firstName
and lastName
attributes to the FIRST_NAME
and LAST_NAME
columns respectively.
- 現(xiàn)代企業(yè)應(yīng)用設(shè)計(jì)指南
- Dreamweaver基礎(chǔ)與實(shí)戰(zhàn)教程
- 鍵盤錄入技術(shù)(第2版)
- Zenoss Core Network and System Monitoring
- 改變思維:菜鳥(niǎo)也能做出震撼PPT(全彩版)
- Photoshop CC 服裝設(shè)計(jì)經(jīng)典實(shí)例教程
- After Effects影視特效立體化教程:After Effects 2021(微課版)
- Spark Cookbook 中文版
- 好用,Excel數(shù)據(jù)處理高手
- Magento: Beginner's Guide
- 原片蛻變:Photoshop CC商業(yè)修圖必修課
- 中文版SolidWorks 2018完全實(shí)戰(zhàn)技術(shù)手冊(cè)
- UML 2.0 in Action: A project/based tutorial
- NHibernate 3 Beginner's Guide
- MSC Fatigue疲勞分析標(biāo)準(zhǔn)教程