JPA Tutorial - JPA Field Mapping Example








When mapping Java bean fields to database column we can choose to mark the fields, mark the getter method and mark both.

Mark the field

The following code is from Professor.java.

It shows how to mark the primary key column to the Java bean field id.

package com.java2s.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();
    }
}

The following code is from PersonDaoImpl.java.

package com.java2s.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;
}


Download Access_Field.zip

Here is the database dump after running the code.


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




Mark the property getter method

The following code is from Professor.java.

The @Id annotation which is for marking which field to be used as the database table primary key column is added to the getter method.

package com.java2s.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();
  }
}


Download Access_Property.zip

The following is the database dump after running the code above.


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




Mixed Marking

With JPA we can mark both field and getter method.

The following code is from Professor.java.

package com.java2s.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;
  }
}

The following code is from PersonDaoImpl.java.

package com.java2s.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;
}


Download 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