📄 customer.java
字号:
package entity;
import javax.persistence.*;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
/**
* This example adds a Address object to the customer.
* Address is represented just as a simple attribut.
*
* @author Antonio Goncalves
*/
@Entity
@Table(name = "t4_customer")
public class Customer {
// ======================================
// = Attributs =
// ======================================
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String firstname;
@Column(nullable = false, length = 30)
private String lastname;
@Column(length = 15)
private String telephone;
@Column(name = "e_mail")
private String email;
@Column(name = "date_of_birth")
@Temporal(TemporalType.DATE)
private Date dateOfBirth;
@Transient
private Integer age;
private Address homeAddress;
// ======================================
// = Constructors =
// ======================================
public Customer() {
}
public Customer(String firstname, String lastname, String telephone, String email, Date dateOfBirth) {
this.firstname = firstname;
this.lastname = lastname;
this.telephone = telephone;
this.email = email;
this.dateOfBirth = dateOfBirth;
}
// ======================================
// = Callback annotated methods =
// ======================================
@PrePersist
@PreUpdate
private void validatePhoneNumber() {
if (telephone == null || "".equals(telephone)) {
return;
}
if (telephone.charAt(0) != '+')
throw new IllegalArgumentException("Invalid phone number");
}
/**
* Calculates the customer's age.
*/
@PostLoad
@PostPersist
@PostUpdate
public void calculateAge() {
if (dateOfBirth == null) {
age = null;
return;
}
Calendar birth = new GregorianCalendar();
birth.setTime(dateOfBirth);
Calendar now = new GregorianCalendar();
now.setTime(new Date());
int adjust = 0;
if (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) {
adjust = -1;
}
age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
}
// ======================================
// = Getters & Setters =
// ======================================
public Long getId() {
return id;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Integer getAge() {
return age;
}
public Date getDateOfBirth() {
return dateOfBirth;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public Address getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(Address homeAddress) {
this.homeAddress = homeAddress;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -