Renaming tables and attributes

So far we assumed that we map classes to database tables having identical names: A Java class User is being mapped to a relational table with identical name User. Sometimes a renaming is desired. We may for example want to access a legacy database by a newly implemented Java application. Choosing meaningful names may conflict with decisions being taken when the original database design took place.

In the following example we change the database tables name from its default User to Person ❶. The properties uidNummber and cname are changed to attribute names numericUid ❷and fullName ❸ respectively:

Java
package hibintro.v8;
   ...
@Entity
@Table(name="Person") ❶
public class User {

    int uidNumber;
    @Id
    @Column(name="numericUid") ❷
    public int getUidNumber() {return uidNumber;}
    public void setUidNumber(int uidNumber) {this.uidNumber = uidNumber;}

    String uid;
    @Column(nullable=false)
    public String getUid() {return uid;}
    public void setUid(String uid) {this.uid = uid;}

    String cname;
    @Column(name="fullName", nullable = false) ❸
    public String getCname() {return cname;}
    public void setCname(String cname) {this.cname = cname;}
    ...
Sql
CREATE TABLE Person ❶ (
   numericUid ❷ int(11) NOT NULL PRIMARY KEY,
   fullName ❸ varchar(255) NOT NULL,
   uid varchar(255) NOT NULL
)