Java教程 第63页
JPA教程 – JPA ID注释示例 来自JPA的@Id注释用于将Java字段标记为数据库表主键列。 public class Person{ @Id private long id; ... 上面的代码将来自Person类的id字段作为数据库中的表person中的主键列。 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Person { @Id private long id; private String name; private String surname; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", surname=" +...
JPA教程 – JPA Transient示例 如果我们不想将属性保存到数据库,我们可以使用@Transient注释标记该字段。 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import java.util.Locale; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Transient; @Entity public class Professor { @Id private int id; private String name; private long salary; @Transient private String convertedName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; convertedName = convertName(name); } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public String getConvertedName() { return convertedName; } public String toString() { return "Employee " + " id: " + getId() + " name: " + getName() + " converted name: " + getConvertedName()...
JPA教程 – JPA日期映射到时间戳示例 我们可以使用以下代码将Java Date类型值映射到TIMESTAMP类型数据库表列。 @Temporal(TemporalType.TIMESTAMP) private java.util.Date dob; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Person { @Id private long id; private String name; @Temporal(TemporalType.TIMESTAMP) private java.util.Date dob; public Person() {} public Person(String name) { this.name = name; } public java.util.Date getDob() { return dob; } public void setDob(java.util.Date dob) { this.dob = dob; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob; } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Calendar; import java.util.List; import...
JPA教程 – JPA日历映射到日期示例 以下部分显示如何将java.util.Calendar映射到数据库日期类型。 @Temporal(TemporalType.DATE) private java.util.Calendar dob; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Person { @Id private long id; private String name; @Temporal(TemporalType.DATE) private java.util.Calendar dob; public Person() {} public Person(String name) { this.name = name; } public java.util.Calendar getDob() { return dob; } public void setDob(java.util.Calendar dob) { this.dob = dob; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob; } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Calendar; import java.util.List; import javax.persistence.EntityManager;...
JPA教程 – JPA日期映射到时间示例 我们可以将Java Date类型值映射到数据库中的TIME类型列。 @Temporal(TemporalType.TIME) private java.util.Date dob; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Temporal; import javax.persistence.TemporalType; @Entity public class Person { @Id private long id; private String name; @Temporal(TemporalType.TIME) private java.util.Date dob; public Person() {} public Person(String name) { this.name = name; } public java.util.Date getDob() { return dob; } public void setDob(java.util.Date dob) { this.dob = dob; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String toString() { return "\n\nID:" + id + "\nName:" + name + "\n\n"+"Dob"+dob; } } 下面的代码来自PersonDaoImpl.java。 package cn.w3cschool.common; import java.util.Calendar; import java.util.List; import...
JPA教程 – JPA日期列定义示例 时间类型是可以在持久状态映射中使用的基于时间的类型集合。 支持的时间类型的列表包括三个java.sql类型,java.sql.Date java.sql.Time和java.sql.Timestamp,以及两个java.util类型,java.util.Date和java.util.Calendar。 在JPA中,我们可以创建数据类型列并设置其默认值。 @Column(name = "START_DATE", columnDefinition = "DATE DEFAULT CURRENT_DATE") private java.sql.Date startDate; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id @Column(name = "EMP_ID") private long id; @Column(length=40) private String name; private String surname; @Column(name = "START_DATE", columnDefinition = "DATE DEFAULT CURRENT_DATE") private java.sql.Date startDate; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public java.sql.Date getStartDate() { return startDate; } public void setStartDate(java.sql.Date startDate) { this.startDate = startDate; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name)...
JPA教程 – JPA列唯一可空例子 我们可以设置映射列是否可以具有空值或者列是否应该具有唯一值。 @Column(unique=true, nullable=false) private String name; 上面的代码将name列设置为unique和nullable。 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id private long id; @Column(unique=true, nullable=false) private String name; private String surname; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } @Override public String toString() { return "Person [id=" + id + ", name=" + name +...
JPA教程 – JPA列精度范围示例 当将Java float或double值映射到数据库表列时,我们可以设置数字类型列的精度尺度。 以下代码将浮点值映射到具有精度8和尺度2的数据库表列。 @Column(precision=8, scale=2) private float hourlyRate; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id private long id; @Column(unique=true, nullable=false) private String name; private String surname; @Column(precision=8, scale=2) private float hourlyRate; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public float getHourlyRate() { return hourlyRate; } public void setHourlyRate(float hourlyRate) { this.hourlyRate = hourlyRate; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String...
JPA教程 – JPA列长度示例 使用JPA,我们可以设置字段的列长度。例如,当将字符串字段映射到VARCHAR时,我们可以在@Column 注释中设置VARCHAR长度。 @Column(length=40) private String name; 例子 下面的代码来自Person.java。 package cn.w3cschool.common; import static javax.persistence.FetchType.LAZY; import javax.persistence.Basic; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Lob; import javax.persistence.Table; @Entity @Table(name="EMP") public class Person { @Id @Column(name = "EMP_ID") private long id; @Column(length=40) private String name; private String surname; public Person() {} public Person(String name, String surname) { this.name = name; this.surname = surname; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSurname() { return surname; } public void setSurname(String surname) { this.surname = surname; } @Override public String toString() { return "Person [id="...
JPA教程 – JPA列名示例 默认情况下,JPA使用字段名作为数据库表列名。我们可以使用Column注释来更改默认值。 以下代码设置id字段以使用EMP_ID作为列名称。 @Column(name = "EMP_ID") private int id; 例子 以下代码来自Professor.java。 package cn.w3cschool.common; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity public class Professor { @Id @Column(name = "EMP_ID") private int id; private String name; @Column(name = "SAL") private long salary; @Column(name = "COMM") private String comments; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public long getSalary() { return salary; } public void setSalary(long salary) { this.salary = salary; } public String getComments() { return comments; } public void setComments(String comments) { this.comments = comments; } public String toString() { return "Employee id: "...