Samples:
One to one
![](http://www.redsoftfactory.com/english/ejb3/docs/images/one2one.jpg)
Country source:
@Entity
@Table(name = "one2oneCountry")
public class Country {
@Id
private Integer id;
private String name;
@OneToOne(optional = true,cascade = CascadeType.ALL, mappedBy = "country")
private Capital capital;
public Country() {
}
public Country(Integer id, String name) {
this.id = id;
this.name = name;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public Capital getCapital() {
return capital;
}
public void setCapital(Capital capital) {
this.capital = capital;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "com.redsoft.samples.Country:[id=" + this.id + ", name="
+ this.name + "]";
}
}
Capital source:
@Entity
@Table(name = "one2oneCapital")
public class Capital {
@Id(generate = GeneratorType.AUTO)
private Integer id;
private String name;
@OneToOne(optional = false, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "COUNTRY_ID", referencedColumnName = "id")
private Country country;
public Capital() {
}
public Capital(String name) {
this.name = name;
}
public Country getCountry() {
return country;
}
public void setCountry(Country country) {
this.country = country;
}
public Integer getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return "com.redsoft.samples.Capital:[id=" + this.id + ", name="
+ this.name + ", country=" + this.country + "]";
}
}
One to many
![](http://www.redsoftfactory.com/english/ejb3/docs/images/one2Many.jpg)
Father source:
@Entity
@Table(name = "O2M_CHILD")
public class Father {
@Id(generate = GeneratorType.AUTO)
private int id;
private String name;
private List<Child> children = new ArrayList<Child>();
public Father() {
}
public Father(String name) {
this.name = name;
}
@OneToMany(targetEntity = Child.class, cascade = CascadeType.ALL, mappedBy = "father")
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
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 void addChild(Child child) {
if (!this.children.contains(child)) {
this.children.add(child);
child.setFather(this);
}
}
public void removeChild(Child child) {
child.setFather(null);
this.children.remove(child);
}
public String toString() {
return "com.redsoft.samples.Father:[id=" + this.id + ", name="
+ this.name + "]";
}
}
Child source:
@Entity
@Table(name = "M2O_CHILD")
public class Child {
@Id(generate = GeneratorType.AUTO)
private int id;
private String name;
private Father father;
public Child() {
}
public Child(String name) {
this.name = name;
}
@ManyToOne
public Father getFather() {
return father;
}
public void setFather(Father father) {
if (father == null) {
this.father = null;
} else {
father.addChild(this);
this.father = father;
}
}
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 String toString() {
return "com.redsoft.samples.Child:[id=" + this.id + ", name="
+ this.name + "]";
}
}
Many to many
![](http://www.redsoftfactory.com/english/ejb3/docs/images/many2many.jpg)
Student source:
@Entity
@Table(name = "samples_M2M_STUDENT")
public class Student {
private int id;
private String name;
private List<Teacher> teachers = new ArrayList<Teacher>();
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Id(generate=GeneratorType.AUTO)
public int getId() {
return id;
}
@ManyToMany(targetEntity = Teacher.class, mappedBy = "students")
public List<Teacher> getTeachers() {
return teachers;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void addTeacher(Teacher teacher) {
if (!this.teachers.contains(teacher)) {
this.teachers.add(teacher);
teacher.addStudent(this);
}
}
public void removeTeacher(Teacher teacher) {
if (this.teachers.contains(teacher)) {
this.teachers.remove(teacher);
teacher.removeStudent(this);
}
}
public String toString(){
return "id["+id+"] name["+name+"]";
}
}
Teacher source:
@Entity
@Table(name = "samples_M2M_TEACHER")
public class Teacher {
private int id;
private String name;
private List<Student> students = new ArrayList<Student>();
public Teacher() {
}
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
@Id(generate=GeneratorType.AUTO)
public int getId() {
return id;
}
@ManyToMany(targetEntity = Student.class, cascade = CascadeType.ALL)
@JoinTable(table = @Table(name = "M2M_TEACHER_STUDENT"),
joinColumns = @JoinColumn(name = "TEACHER_ID", referencedColumnName = "ID"),
inverseJoinColumns = @JoinColumn(name = "STUDENT_ID", referencedColumnName = "ID"))
public List<Student> getStudents() {
return students;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setStudents(List<Student> students) {
this.students = students;
}
public void addStudent(Student student) {
if(!this.students.contains(student)){
this.students.add(student);
student.addTeacher(this);
}
}
public boolean equals(Object o) {
if (this == o)
return true;
if (!(o instanceof Teacher))
return false;
final Teacher teacher = (Teacher) o;
if (id != teacher.getId())
return false;
if (name == null) {
if (teacher.getName() != null)
return false;
} else {
if (!name.equals(teacher.getName()))
return false;
}
if (!students.equals(teacher.getStudents()))
return false;
return true;
}
public int hashCode() {
int result = 0;
if (name != null) {
result = name.hashCode();
}
result = 29 * result + id;
result = 29 * result + students.hashCode();
return result;
}
public void removeStudent(Student student) {
if(this.students.contains(student)){
students.remove(student);
student.removeTeacher(this);
}
}
public String toString(){
return "id["+id+"] name["+name+"]";
}
}