JPA Tutorial - JPA OneToMany Unidirectional Example








The following code shows how to do unidirectional one to many mapping.

Example

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.setName("name");
    emp.setSalary(12345);
    

    Phone p = new Phone();
    p.setNumber("1234509876");;

    emp.addPhone(p);
    em.persist(p);
    em.persist(emp);
  }

  @PersistenceContext
  private EntityManager em;
}

The following code is from Professor.java.

package com.java2s.common;

import java.util.ArrayList;
import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;

@Entity
public class Professor {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    private long salary;
    
    @OneToMany
    @JoinTable(name="EMP_PHONE", 
          joinColumns=@JoinColumn(name="EMP_ID"),
          inverseJoinColumns=@JoinColumn(name="PHONE_ID"))
    private Collection<Phone> phones;

    public Professor() {
        phones = new ArrayList<Phone>();
    }

    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 void addPhone(Phone phone) {
        if (!getPhones().contains(phone)) {
            getPhones().add(phone);
        }
    }
    
    public Collection<Phone> getPhones() {
        return phones;
    }
    
    public String toString() {
        return "Employee id: " + getId() + " name: " + getName() + 
               " with " + getPhones().size() + " phones";
    }
}

The following code is from Phone.java.

package com.java2s.common;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Phone {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    @Column(name="NUM")
    private String number;
    private String type;
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getNumber() {
        return number;
    }
    
    public void setNumber(String phoneNo) {
        this.number = phoneNo;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String phoneType) {
        this.type = phoneType;
    }

    public String toString() {
        return "Phone id: " + getId() + 
               ", no: " + getNumber() +
               ", type: " + getType();
    }
}


Download OneToMany_Unidirectional.zip

The following is the database dump.

Table Name: EMP_PHONE
 Row:
    Column Name: EMP_ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: PHONE_ID,
    Column Type: INTEGER:
    Column Value: 1





Table Name: PHONE
 Row:
    Column Name: ID,
    Column Type: INTEGER:
    Column Value: 1

    Column Name: NUM,
    Column Type: VARCHAR:
    Column Value: 1234509876

    Column Name: TYPE,
    Column Type: VARCHAR:
    Column Value: null





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