我试图了解使用 Struts2、Hibernate 和 EJB 的 JavaEE 架构的基本架构。
我有一个简单的应用程序,可以添加员工并列出他们。
到目前为止,我在 Eclipse 中做了 3 个项目:
- 包含链接到数据库的模型的 JPA 项目
- 处理服务层的 EJB 项目
- Struts 2 的动态 Web 项目
动态 Web 项目 > controller.EmployeeAction.java:
import local.EmployeeService;
import model.Employee;
public class EmployeeAction extends ActionSupport {
private Employee employee;
private Collection<Employee> employeeList;
@EJB
private EmployeeService employeeManager;
public String execute() {
this.employeeList = employeeManager.findAll();
return SUCCESS;
}
public String add() {
employeeManager.addItem(this.employee);
this.employeeList = employeeManager.findAll();
return SUCCESS;
}
[...]
}
EJB 项目 > local.EmployeeService.java:
import model.Employee;
@Stateless
public class EmployeeService {
@PersistenceContext
private EntityManager em;
public void addItem(Employee i) {
em.persist(i);
}
public Collection<Employee> findAll() {
Query query = em.createQuery("SELECT e FROM Employee e");
return (Collection<Employee>) query.getResultList();
}
}
JPA 项目 > 模型。员工:
@Entity
@Table(name="\"Employee\"")
@NamedQuery(name="Employee.findAll", query="SELECT e FROM Employee e")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(unique=true, nullable=false)
private Integer idemployee;
@Column(length=255)
private String password;
@Column(nullable=false, length=255)
private String username;
public Employee() {
}
public Integer getIdemployee() {
return this.idemployee;
}
public void setIdemployee(Integer idemployee) {
this.idemployee = idemployee;
}
[...]
}
但是当我尝试添加员工时,我得到一个 ClassNotFoundException for model.Employee
.
类路径似乎不错。我错过了什么吗?