Java Tutorial/JPA/One To Many Mapping
Версия от 17:44, 31 мая 2010; (обсуждение)
Содержание
- 1 One To Many Bidirectional
- 2 One To Many Cascade.All
- 3 One To Many Join Table Setup
- 4 One To Many List Collection
- 5 One To Many Map Collection
- 6 One To Many With Mapped By To remove Mapping Table
- 7 One To Many With Order By Setting
- 8 Set target Entity For One To Many Mapping
- 9 Use Collection In One To Many Mapping
One To Many Bidirectional
File: Department.java
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department")
private Collection<Student> students;
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 Collection<Student> getStudents() {
return students;
}
public void setStudent(Collection<Student> students) {
this.students = students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
One To Many Cascade.All
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.ManyToOne;
@Entity
public class Student {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@ManyToOne(cascade=CascadeType.ALL)
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";
}
}
One To Many Join Table Setup
File: Phone.java
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();
}
}
One To Many List Collection
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 "Student id: " + getId() + " name: " + getName() +
" with " + getDepartment();
}
}
One To Many Map Collection
File: Department.java
import java.util.HashMap;
import java.util.Map;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MapKey;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department")
@MapKey(name="name")
private Map<String, Student> students;
public Department() {
students = new HashMap<String, Student>();
}
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 void addStudent(Student student) {
if (!getStudents().containsKey(student.getName())) {
getStudents().put(student.getName(), student);
if (student.getDepartment() != null) {
student.getDepartment().getStudents().remove(student.getName());
}
student.setDepartment(this);
}
}
public Map<String,Student> getStudents() {
return students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
One To Many With Mapped By To remove Mapping Table
File: Department.java
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department",cascade=CascadeType.ALL)
private Set<Student> students;
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 Set<Student> getStudents() {
return students;
}
public void setStudent(Set<Student> students) {
this.students = students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName()+"Student:"+ students;
}
}
One To Many With Order By Setting
File: Department.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department")
@OrderBy("name ASC")
private List<Student> students;
public Department() {
students = new ArrayList<Student>();
}
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 void addStudent(Student student) {
if (!getStudents().contains(student)) {
getStudents().add(student);
if (student.getDepartment() != null) {
student.getDepartment().getStudents().remove(student);
}
student.setDepartment(this);
}
}
public Collection<Student> getStudents() {
return students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}
Set target Entity For One To Many Mapping
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.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 ;
}
}
Use Collection In One To Many Mapping
File: Department.java
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
@OneToMany(mappedBy="department")
private Collection<Student> students;
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 Collection<Student> getStudents() {
return students;
}
public void setStudent(Collection<Student> students) {
this.students = students;
}
public String toString() {
return "Department id: " + getId() +
", name: " + getName();
}
}