JPA 字段映射示例

由 haiguiking 创建,youj 最后一次修改 2017-01-09

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


以上内容是否对您有帮助:
二维码
建议反馈
二维码