JPA 教程 – JPA 字段映射示例
当将 Java bean 字段映射到数据库列时,我们可以选择标记字段,标记 getter 方法并标记两者。
标记字段
以下代码来自 Professor.java。
它显示如何将主键列标记为 Java bean 字段标识。
package cn.w3cschool.common;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Professor {
@Id
private int id;
private String name;
private long salary;
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 toString() {
return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
}
}
下面的代码来自 PersonDaoImpl.java。
package cn.w3cschool.common;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class PersonDaoImpl {
public void test() {
Professor emp = new Professor();
emp.setId(1);
emp.setName("name");
emp.setSalary(12345);
em.persist(emp);
}
@PersistenceContext
private EntityManager em;
}
下载 Access_Field.zip
这里是运行代码后的数据库转储。
Table Name: PROFESSOR
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 1
Column Name: NAME,
Column Type: VARCHAR:
Column Value: name
Column Name: SALARY,
Column Type: BIGINT:
Column Value: 12345
标记属性 getter 方法
以下代码来自 Professor.java。
用于标记要用作数据库表主键列的字段的 @Id 注释将添加到 getter 方法。
package cn.w3cschool.common;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Professor {
private int id;
private String name;
private long wage;
@Id
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 wage;
}
public void setSalary(long salary) {
this.wage = salary;
}
public String toString() {
return "Employee id: " + getId() + " name: " + getName() + " salary: " + getSalary();
}
}
下载 Access_Property.zip
以下是在运行上面的代码后的数据库转储。
Table Name: PROFESSOR
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 1
Column Name: NAME,
Column Type: VARCHAR:
Column Value: name
Column Name: SALARY,
Column Type: BIGINT:
Column Value: 12345
混合标记
使用 JPA,我们可以标记字段和 getter 方法。
以下代码来自 Professor.java。
package cn.w3cschool.common;
import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Transient;
@Access(AccessType.FIELD)
@Entity
public class Professor {
public static String LOCAL_AREA_CODE = "999";
@Id
private int id;
@Transient
private String phoneNum;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhoneNumber() {
return phoneNum;
}
public void setPhoneNumber(String num) {
this.phoneNum = num;
}
@Access(AccessType.PROPERTY)
@Column(name = "PHONE")
protected String getPhoneNumberForDb() {
if (null != phoneNum && phoneNum.length() == 10)
return phoneNum;
else
return LOCAL_AREA_CODE + phoneNum;
}
protected void setPhoneNumberForDb(String num) {
if (num.startsWith(LOCAL_AREA_CODE))
phoneNum = num.substring(3);
else
phoneNum = num;
}
}
下面的代码来自 PersonDaoImpl.java。
package cn.w3cschool.common;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public class PersonDaoImpl {
public void test() {
Professor emp = new Professor();
emp.setId(1);
emp.setPhoneNumber("123456789");
em.persist(emp);
}
@PersistenceContext
private EntityManager em;
}
下载 Access_Mixed.zip
Table Name: PROFESSOR
Row:
Column Name: ID,
Column Type: INTEGER:
Column Value: 1
Column Name: PHONE,
Column Type: VARCHAR:
Column Value: 999123456789