public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "LAST_NAME")
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Temporal(TemporalType.DATE)
@Column(name = "BIRTH_DATE")
public Date getBirthDate() {
return this.birthDate;
}
public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;
}
public String toString() {
return "Contact - Id: " + id + ", First name: " + firstName
+ ", Last name: " + lastName + ", Birthday: " + birthDate;
}
}
First, we annotate the type with @Entity, which means that this is a mapped entity class. The @Table
annotation defines the table name in the database that this entity was being mapped to. For each
mapped attribute, we annotate with the @Column annotation, with the column names provided. You can
skip the table and column names in case the type and attribute names are exactly the same as the table
and column names.
About the mappings, we would like to highlight a few points:
For the birth date attribute, we annotate it with @Temporal, with the TemporalType
·
DATE assigned. This means we would like to map the data type from the Java
date type (java.util.Date) to the SQL data type (java.sql.Date). So, we can
access the attribute birthDate in Contact object using java.util.Date as usual in
our application.
For the id attribute, we annotate it with @Id. This means it's the primary key of the
·
object. Hibernate will use it as the unique identifier when managing the contact
entity instances within its session. On the other hand, the @GeneratedValue
annotation tells Hibernate how the id value was generated. The IDENTITY (we can
use it directly within the annotation because of the import static statement)
strategy means that the id was generated by the backend (the ID column of the
CONTACT table is the primary key, with AUTO_INCREMENT specified, which means that
the value will be generated and assigned by the database during insert operation)
during insert.
For the version attribute, we annotate it with @Version. This instructs Hibernate
·
that we would like to use an optimistic locking mechanism, using the version
attribute as a control. Every time Hibernate updates a record, it will compare the
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home