如何使用 JPA 和 Facade?
How to work with JPA and Facade?
我对 JPA 和 Facade 模型有疑问,因为我现在不知道如何使用它。我有一个名为 "Kandidatenstamm" 的 class(这是一个实体)。对于这个 Kandidatenstamm,存在 Facade 和 Controller。还有一个抽象外观和一个抽象控制器。那么我必须在一个 bean 中做什么,例如,从数据库中获取一个 id=4 的对象?
这是我的代码
抽象立面
public abstract class AbstractFacade<T> {
private 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();
}
}
KandidatenstammFacade
@Stateless
public class KandidatenstammFacade extends AbstractFacade<Kandidatenstamm> {
@PersistenceContext(unitName = "ffswebadministrationPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public KandidatenstammFacade() {
super(Kandidatenstamm.class);
}
}
好的,我自己搞定了。仅进行 EJB 注入并使用 Facade。这里以 "Personenstamm" 为例(原则上与 "Kandidatenstamm" 相同)
@EJB
private PersonenstammFacade personenstammFacade;
....
List <Personenstamm> personlist;
personlist = personenstammFacade.findAll();
....
//do what you want with this list
我对 JPA 和 Facade 模型有疑问,因为我现在不知道如何使用它。我有一个名为 "Kandidatenstamm" 的 class(这是一个实体)。对于这个 Kandidatenstamm,存在 Facade 和 Controller。还有一个抽象外观和一个抽象控制器。那么我必须在一个 bean 中做什么,例如,从数据库中获取一个 id=4 的对象?
这是我的代码
抽象立面
public abstract class AbstractFacade<T> {
private 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();
}
}
KandidatenstammFacade
@Stateless
public class KandidatenstammFacade extends AbstractFacade<Kandidatenstamm> {
@PersistenceContext(unitName = "ffswebadministrationPU")
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
public KandidatenstammFacade() {
super(Kandidatenstamm.class);
}
}
好的,我自己搞定了。仅进行 EJB 注入并使用 Facade。这里以 "Personenstamm" 为例(原则上与 "Kandidatenstamm" 相同)
@EJB
private PersonenstammFacade personenstammFacade;
....
List <Personenstamm> personlist;
personlist = personenstammFacade.findAll();
....
//do what you want with this list