Liberator EJB3 Persistence Engine samples

Samples:

One to one

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

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

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+"]";
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值