我正在使用Netbeans 8.2,JSF 2.2,JPA,EclipseLink,Maven和Primefaces做一个项目。
我正在使用的模式是Facade。
出现的问题是我无法保留数据。我创建了一个简单的xhtml,还使用一个简单的POJO进行了测试,但是它并没有持久。
奇怪的是,我开始使用Payara带来的Glassfish 4.1.1(因为Glassfish的原始4.1.1提出了要爱连接池的问题)和MySQL 8,然后我尝试了相同的应用程序服务器,但是在PostgreSQL和这两种情况下,我都可以创建一个有效连接的有效池,这使两个数据库都没有问题,但是当我运行该应用程序时,数据不会持久。
然后,我对WildFly 12.0.0进行了完全相同的测试。我尝试为MySQL 8创建一个连接池,为PostreSQL创建另一个连接池。检查两者是否与数据库连接良好,但它们不会持久存在。
我注意到在任何情况下,两个应用程序服务器都不会在运行时抛出错误,就像一切都正确一样。
我很困惑。
在此先感谢您的关注。
POJO代码如下:
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "categoria")
public class Categoria implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int codigo;
@Column(name = "nombre")
private String nombre;
@Column(name = "estado")
private boolean estado = true;
public int getCodigo() {
return codigo;
}
public void setCodigo(int codigo) {
this.codigo = codigo;
}
public boolean getEstado() {
return estado;
}
public void setEstado(boolean estado) {
this.estado = estado;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
}
控制器代码:
import com.mitocode.ejb.CategoriaFacadeLocal;
import com.mitocode.model.Categoria;
import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
@Named
@ViewScoped
public class CategoriaController implements Serializable{
@EJB
private CategoriaFacadeLocal categoriaEJB;
private Categoria categoria;
@PostConstruct
public void init(){
categoria = new Categoria();
}
public void registrar(){
try{
categoriaEJB.create(categoria);
}
catch(Exception e){
//Mostrar mensaje de error de PrimeFaces (Growl)
}
}
public Categoria getCategoria() {
return categoria;
}
public void setCategoria(Categoria categoria) {
this.categoria = categoria;
}
}
查看代码:
<?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://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<p:inputText label="Ingrese Categoría" value="#{categoriaController.categoria.nombre}" />
<p:commandButton value = "Registrar" actionListener = "#{categoriaController.registrar()}"/>
</h:body>
</html>
Persistence.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="primePU" transaction-type="JTA">
<jta-data-source>jdbc/primePG</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="none"/>
</properties>
</persistence-unit>
</persistence>
AbstractFacade代码:
import java.util.List;
import javax.persistence.EntityManager;
public abstract class AbstractFacade<T> {
final Class<T> entityClass;
public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}
protected abstract EntityManager getEntityManager();
public void create(T entity) {
getEntityManager().persist(entity);
}
public void edit(T entity) {
getEntityManager().merge(entity);
}
public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}
public T find(Object id) {
return getEntityManager().find(entityClass, id);
}
public List<T> findAll() {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
return getEntityManager().createQuery(cq).getResultList();
}
public List<T> findRange(int[] range) {
javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
cq.select(cq.from(entityClass));
javax.persistence.Query q = getEntityManager().createQuery(cq);
q.setMaxResults(range[1] - range[0] + 1);
q.setFirstResult(range[0]);
return q.getResultList();
}
public int count() {
javax.persistence.criteria.CriteriaQuery cq =
getEntityManager().getCriteriaBuilder().createQuery();
javax.persistence.criteria.Root<T> rt = cq.from(entityClass);
cq.select(getEntityManager().getCriteriaBuilder().count(rt));
javax.persistence.Query q = getEntityManager().createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
}
}
接口CategoriaFacade本地代码:
import java.util.List;
import javax.ejb.Local;
@Local
public interface CategoriaFacadeLocal {
void create(Categoria categoria);
void edit(Categoria categoria);
void remove(Categoria categoria);
Categoria find(Object id);
List<Categoria> findAll();
List<Categoria> findRange(int[] range);
int count();
}
EJB CategoriaFacade代码:
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
public class CategoriaFacade extends AbstractFacade<Categoria> implements
CategoriaFacadeLocal {
@PersistenceContext(unitName = "primePU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public CategoriaFacade() {
super(Categoria.class);
}
}