Friday, April 6, 2012

EJB Example

Birici Adim

public class Employee {
private int id;
private String name;
private long salary;

public Employee() {}

public Employee(int id) {
this.id = id;
}

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 long getSalary() {
return salary;
}

public void setSalary (long salary) {
this.salary = salary;
}
}

Ikinci Adim

@Entity
public class Employee {
@Id
private int id;
private String name;
private long salary;

public Employee() {}

public Employee(int id) {
this.id = id;
}

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 long getSalary() {
return salary;
}

public void setSalary (long salary) {
this.salary = salary;
}
}

Ucuncu Adim



Dorduncu Adim

@Stateless
public class EmployeeBean implements EmployeeBeanLocal {

@PersistenceContext(unitName = "EJB_ApplicationPU")
EntityManager em;


}

Besinci Adim

@Stateless
public class EmployeeBean implements EmployeeBeanLocal {

@PersistenceContext(unitName = "EJB_ApplicationPU")
EntityManager em;

@Override
public List listEmployee() {
try {
return em.createQuery("Select a From Employee a").getResultList();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

}


Altinci Adim

@Stateless
public class EmployeeBean implements EmployeeBeanLocal {

@PersistenceContext(unitName = "EJB_ApplicationPU")
EntityManager em;

@Override
public List listEmployee() {
try {
return em.createQuery("Select a From Employee a").getResultList();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public Employee createEmployee(int id, String name, long salary) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}


}


Yedinci Adim

@Stateless
public class EmployeeBean implements EmployeeBeanLocal {

@PersistenceContext(unitName = "EJB_ApplicationPU")
EntityManager em;

@Override
public List listEmployee() {
try {
return em.createQuery("Select a From Employee a").getResultList();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public Employee createEmployee(int id, String name, long salary) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}

@Override
public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}



}


Sekizinci Adim

@Stateless
public class EmployeeBean implements EmployeeBeanLocal {

@PersistenceContext(unitName = "EJB_ApplicationPU")
EntityManager em;

@Override
public List listEmployee() {
try {
return em.createQuery("Select a From Employee a").getResultList();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

@Override
public Employee createEmployee(int id, String name, long salary) {
Employee emp = new Employee(id);
emp.setName(name);
emp.setSalary(salary);
em.persist(emp);
return emp;
}

@Override
public Employee findEmployee(int id) {
return em.find(Employee.class, id);
}

public void removeEmployee(int id) {
Employee emp = em.find(Employee.class, id);
if (emp != null) {
em.remove(emp);
}
}

}


Dokuzuncu Adim

@Local
public interface EmployeeBeanLocal {
List listEmployee();
}


Onuncu Adim

@EJB
EmployeeBeanLocal local;


Onbirinci Adim

public EmployeeClient{
private List list = new ArrayList();
@EJB
EmployeeBeanLocal local;


public List getList() {
list = local.listEmployee();
return list;
}

public void setList(List list) {
this.list = list;
}
}

No comments:

Post a Comment