Java Tutorial/JPA/Many to One Mapping

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Many To One Cascade Merge

File: Department.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    public String toString() {
        return "Department id: " + getId() + 
               ", name: " + getName();
    }
}





Many To One Cascade Remove

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
    @Id
    private int id;
    private String name;
    
    @ManyToOne(cascade=CascadeType.REMOVE)
    Address address;
    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 Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address; 
    }
    public String toString() {
        return "Student id: " + getId() + " name: " + getName() +
               " with " + getAddress();
    }
}





Many To One Cascasde Persist

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
    @Id
    private int id;
    private String name;
    
    @ManyToOne(cascade=CascadeType.PERSIST)
    Address address;
    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 Address getAddress() {
        return address;
    }
    
    public void setAddress(Address address) {
        this.address = address; 
    }
    public String toString() {
        return "Student id: " + getId() + " name: " + getName() +
               " with " + getAddress();
    }
}





Many To One Insertable Nullable Updatable

File: Department.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String name;
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String deptName) {
        this.name = deptName;
    }
    public String toString() {
        return "Department id: " + getId() + 
               ", name: " + getName();
    }
}





Many To One Mapping Manually Linking

File: Student.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  @ManyToOne
  private Department department;
  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 Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n";
  }
}





Many To One Mapping Unique Join Column

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Student {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  @ManyToOne(cascade=CascadeType.ALL)
  @JoinColumn(name="DEPT_ID", unique= true, nullable=true, insertable=true, updatable=true)
  private Department department;
  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 Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n" + department;
  }
}





Many to One Map With Entity Hierarchy

File: Employee.java



import java.util.Date;
import javax.persistence.AssociationOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Employee {
  @Id
  private int id;
  private String name;
  @Temporal(TemporalType.DATE)
  private Date startDate;
  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 Date getStartDate() {
    return startDate;
  }
  public void setStartDate(Date startDate) {
    this.startDate = startDate;
  }
  public String toString() {
    return "Employee id: " + getId() + " name: " + getName();
  }
}
@MappedSuperclass
abstract class CompanyEmployee extends Employee {
  private int vacation;
  @ManyToOne
  private Employee manager;
  public int getVacation() {
    return vacation;
  }
  public void setVacation(int vacation) {
    this.vacation = vacation;
  }
  public Employee getManager() {
    return manager;
  }
  public void setManager(Employee manager) {
    this.manager = manager;
  }
}
@Entity
@AssociationOverride(name = "manager", joinColumns = @JoinColumn(name = "MGR"))
class PartTimeEmployee extends CompanyEmployee {
  private float hourlyRate;
  public float getHourlyRate() {
    return hourlyRate;
  }
  public void setHourlyRate(float hourlyRate) {
    this.hourlyRate = hourlyRate;
  }
  public String toString() {
    return "PartTimeEmployee id: " + getId() + " name: " + getName();
  }
}
@Entity
class FullTimeEmployee extends CompanyEmployee {
  private long salary;

  private long pensionContribution;
  public long getPensionContribution() {
    return pensionContribution;
  }
  public void setPensionContribution(long pension) {
    this.pensionContribution = pension;
  }
  public long getSalary() {
    return salary;
  }
  public void setSalary(long salary) {
    this.salary = salary;
  }
  public String toString() {
    return "FullTimeEmployee id: " + getId() + " name: " + getName();
  }
}





Many To One Set Join Column Name

File: Student.java



import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
@Entity
public class Student {
  @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
  private int id;
  private String name;
  @ManyToOne(cascade=CascadeType.ALL)
  @JoinColumn(name="DEPT_ID")
  private Department department;
  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 Department getDepartment() {
    return department;
  }
  public void setDepartment(Department department) {
    this.department = department;
  }
  public String toString() {
    return "\n\nID:" + id + "\nName:" + name + "\n\n" + department;
  }
}





Mapping Relationship State

File: Project.java



import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Project {
    @Id private int id;
    private String name;
    
    @OneToMany(mappedBy="project")
    private Collection<ProjectAssignment> assignments;
    public Project() {
        assignments = new ArrayList<ProjectAssignment>();
    }
    
    public int getId() {
        return id;
    }
    
    public void setId(int projectNo) {
        this.id = projectNo;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String projectName) {
        this.name = projectName;
    }
    
    public Collection<ProjectAssignment> getAssignments() {
        return assignments;
    }
    
    public String toString() {
        return " no: " + getId() + 
                ", name: " + getName();
    }
}