我正在使用 EJB JPA MySQL netbeans7.2 开发一个小型应用程序,因此我的应用程序有以下目标:获取名称及其 ID 列表并将其他名称添加到列表中。
我的课程是:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.rachid.entities;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
@Entity
@Table(name = "employee")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Employee.findAll", query = "SELECT e FROM Employee e"),
@NamedQuery(name = "Employee.findById", query = "SELECT e FROM Employee e WHERE
e.id = :id"),
@NamedQuery(name = "Employee.findByName", query = "SELECT e FROM Employee e WHERE
e.name = :name")})
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Size(max = 20)
@Column(name = "name")
private String name;
public Employee() {
}
public Employee(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Employee)) {
return false;
}
Employee other = (Employee) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
return "com.rachid.entities.Employee[ id=" + id + " ]";
}
}
使用该实体的会话 bean 是:
package com.rachid.session;
import com.rachid.entities.Employee;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
@Stateless
@LocalBean
public class EmployeeSessionBean {
@PersistenceContext(unitName = "EnterpriseApplication1-ejbPU")
private EntityManager em;
public List<Employee> getAllEmployees() {
Query query = em.createNamedQuery("Employee.findAll");
return query.getResultList();
}
// Add business logic below. (Right-click in editor and choose
// "Insert Code > Add Business Method")
public Employee updateEmployee(Employee parameter) {
return em.merge(parameter);
}
public void addEmployee(Employee employee){
em.persist(employee);
}
}
托管 bean 是:
package managedBeans;
import com.rachid.entities.Employee;
import com.rachid.session.EmployeeSessionBean;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
@Named(value = "employeeManagedBean")
@ViewScoped
public class EmployeeManagedBean implements Serializable {
public EmployeeManagedBean() {
}
@EJB
private EmployeeSessionBean employeeBean;
private Employee employee;
private Employee newEmployee;
private List<Employee> employees;
public List<Employee> getEmployees(){
employees = new ArrayList<Employee>();
return employeeBean.getAllEmployees();
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public void add(){
if(newEmployee!= null){
employeeBean.addEmployee(newEmployee);
}
}
public String update(){
System.out.println("***UPDATE***");
employee = employeeBean.updateEmployee(employee);
return "Employees List";
}
public Employee getNewEmployee() {
return newEmployee;
}
public void setNewEmployee(Employee newEmployee) {
this.newEmployee = newEmployee;
}
}
不幸的是,当我在 jsf 页面中调用 addEmployee 方法时,没有任何内容写入数据库。我担心容器处理的事务管理;也就是说,更新和插入是由容器管理的,它可能不会立即写入数据库,所以我用 @TransationalAttribute(TransactionalAttributeType.REQUIRED) 注释了 addEmployee 方法(在 EJB 中),但结果仍然相同。
我的代码或我对问题上下文的理解是否缺少任何内容?
(请注意,我是 EJB 的初学者 :))
以下是 jsf 页面(一个显示列表的索引页面和另一个用于向列表添加新元素的页面):
index.xhtml 文件:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
Hello from Facelets
<f:view>
<h:form>
<h1><h:outputText value="List"/></h1>
<h:dataTable value="#{employeeManagedBean.employees}" var="item">
<h:column>
<f:facet name="header">
<h:outputText value="Id"/>
</f:facet>
<h:outputText value="#{item.id}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
<h:outputText value="#{item.name}"/>
</h:column>
</h:dataTable>
</h:form>
</f:view>
<h:form>
<h:commandButton value="nouveau" action="ajouter.xhtml" type="submit" />
</h:form>
</h:body>
ajouter.xhtml 文件:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<h:body>
<f:view>
<h:form>
<h1><h:outputText value="Create/Edit"/></h1>
<h:panelGrid columns="2">
<h:outputLabel value="Id:" for="id" />
<h:inputText id="id" value="#{employeeManagedBean.newEmployee.id}"
title="Id"
required="true" requiredMessage="The Id field is required."/>
<h:outputLabel value="Name:" for="name" />
<h:inputText id="name" value="#{employeeManagedBean.newEmployee.name}"
title="Name" />
</h:panelGrid>
</h:form>
</f:view>
<h:form>
<h:commandButton value="ajouter" action="index.xhtml" type="submit"
actionListener="#{employeeManagedBean.add()}" />
</h:form>
</h:body>