Now, you need to create and implement the Address and Student classes, and the session bean StudsBean that defines the business methods required in our sample scenario.
Creating and Implementing the Address class
In the Project Explorer, expand your EJB DC
In the context menu of ejbModule , choose New → Class.
Enter com.sap.tc.cm.ejb.example in the Package field. Enter Address in the Name field. Choose Finish . You have created the Address class.
To implement the class, open the Java editor and add the following source code:
package com.sap.tc.cm.ejb.example;
public class Address {
private String town;
private String street;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getTown() {
return town;
}
public void setTown(String town) {
this.town = town;
}
}
Creating and Implementing the Student class
In an analogous manner, create a Student class.
To implement it, use the following source code:
package com.sap.tc.cm.ejb.example;
public class Student {
private String name;
private Address address;
private int age;
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Creating and Implementing the Session Bean
In the Project Explorer , expand your EJB DC.
In the context menu of ejbModule , choose New → Other . Expand EJB and choose EJB Session Bean 3.0 . Choose Next .
Enter StudsBean in the EJB Name field and com.sap.tc.cm.ejb.example in the Default EJBPackage field . Set the Local option and choose Next .
The next screen confirms that the Business Interface StudsLocal is automatically created. Choose Finish
To implement the StudsBean session bean, use the following source code:
package com.sap.tc.cm.ejb.example;
import javax.ejb.Stateless;
@Stateless(name="StudsBean")
public class StudsBean implements StudsLocal {
public Student getStudent(String name) {
// creating new Student
Student student = new Student();
student.setName(name);
student.setAge(25);
// creating new Address and setting it to the Student
Address a = new Address();
a.setTown("MyTown");
a.setStreet("MyStreet");
student.setAddress(a);
return student;
}
}
Switch to StudsLocal.java and insert the following source code:
package com.sap.tc.cm.ejb.example;
import javax.ejb.Local;
@Local
public interface StudsLocal {
public Student getStudent(String name);
}
You have created and implemented all classes and interfaces you need for your EJB DC.