Tutorial for OpenXava 3
Welcome to this short voyage to start to use OpenXava.
These instructions suposses that you have the OpenXava Tomcat started (start-tomcat.sh o .bat has been executed) and you Eclipse is opened against the workspace included in OpenXava distribution.
Execute example application MySchool
OpenXava distribution comes with an example application called
MySchool, in order to execute it follow the next steps, using ant targets. The ant targets used are set in Eclipse, so to execute it only it is needed to click on arrow of External Tools button
Modify example application MySchool
Add other component and its module
Copy and paste
src/org.openxava.school.model/Teacher.java file with the new name
Pupil.java.
Change Pupil.java until it remains:
package org.openxava.school.model;
import javax.persistence.*;
import org.openxava.annotations.*;
@Entity
public class Pupil {
@Id @Column(length=2) @Required
private int number;
@Column(length=40) @Required
private String name;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Now only that is needed to redeploy.
The ant targets used are set in Eclipse, so to execute it only it is needed to click on arrow of External Tools button
That pupils know about his teachers
It is easy to make a pupil to have a reference to a teacher. Add the next code to
Pupil class :
@ManyToOne
private Teacher teacher;
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
And now to test it:
As you see, plain vanilla.
If you want the reference to be displayed in combo format, you should add
@DescriptionsList to
teacher reference in
Pupil, as following:
@ManyToOne @DescriptionsList
private Teacher teacher;
Note the
@DescriptionsList annotation.
Now, you have a
Pupil class as the next one:
package org.openxava.school.model;
import javax.persistence.*;
import org.openxava.annotations.*;
@Entity
public class Pupil {
@Id @Column(length=2) @Required
private int number;
@Column(length=40) @Required
private String name;
@ManyToOne @DescriptionsList
private Teacher teacher;
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher getTeacher() {
return teacher;
}
public void setTeacher(Teacher teacher) {
this.teacher = teacher;
}
}
Now deploy again:
And now let's go to put a pupils collection in teacher. First, add the next package import to your Teacher class:
import java.util.*;
And then put the collection declaration inside the class:
@OneToMany(mappedBy="teacher")
private Collection<Pupil> pupils;
public Collection<Pupil> getPupils() {
return pupils;
}
public void setPupils(Collection<Pupil> pupils) {
this.pupils = pupils;
}
After these changes the
Teacher class remains in this way:
package org.openxava.school.model;
import java.util.*;
import javax.persistence.*;
import org.openxava.annotations.*;
@Entity
public class Teacher {
@Id @Column(length=5) @Required
private String id;
@Column(length=40) @Required
private String name;
@OneToMany(mappedBy="teacher")
private Collection<Pupil> pupils;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Pupil> getPupils() {
return pupils;
}
public void setPupils(Collection<Pupil> pupils) {
this.pupils = pupils;
}
}
And deploy again:
Execute junit test of MySchool application
Go to your Eclipse and in your
MySchool project:
- Go to TeacherTest class (in src/org.openxava.school.tests) and with right button choose Run > JUnit Test
The OpenXava junit test allows to test a module like a real user with an Internet navigator. So it is possible to automatize the test application process. Your are encouraged to examine the
TeachersTest code for make idea of how create a junit test for OpenXava. If you want to develop quality applications is essential to become fond to junit test.
That's all, folks
In this example you can see that little changes imply little effort and the effect is seen on instantaneous form.
Help
If you wish to have an idea of all OpenXava features, that surpass what you see in this naive example, you need to take a look to
OpenXavaTest project; specially to
ox3-src/org.openxava.test.model package.
You can obtain help for executing this tutorial or for any other problem with OpenXava in:
http://sourceforge.net/forum/forum.php?forum_id=419690
You can learn more things about OpenXava at: