gpt4 book ai didi

java - 来自抽象、通用外观的 JPA Controller 类的具体外观

转载 作者:行者123 更新时间:2023-12-01 12:15:37 25 4
gpt4 key购买 nike

我有一个few classes very like the following LinkFacade。如何制作“通用”外观来消除或至少减少在不同“外观”类之间重复的方法?

“外观”示例:

package net.bounceme.dur.jpa.facades;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import net.bounceme.dur.jpa.controllers.LinkJpaController;
import net.bounceme.dur.jpa.entities.Link;
import net.bounceme.dur.jpa.exceptions.NonexistentEntityException;

public class LinkFacade {

private final static Logger log = Logger.getLogger(LinkFacade.class.getName());
private final EntityManagerFactory emf = Persistence.createEntityManagerFactory("SeleniumIteratorPU");
private final LinkJpaController controller = new LinkJpaController(emf);

public LinkFacade() {
}

public void create(Link link) {
controller.create(link);
}

public void edit(Link link) {
try {
controller.edit(link);
} catch (NonexistentEntityException ex) {
Logger.getLogger(LinkFacade.class.getName()).log(Level.SEVERE, null, ex);
} catch (Exception ex) {
Logger.getLogger(LinkFacade.class.getName()).log(Level.SEVERE, null, ex);
}
}

public void destroy(int id) {
try {
controller.destroy(id);
} catch (NonexistentEntityException ex) {
Logger.getLogger(LinkFacade.class.getName()).log(Level.SEVERE, null, ex);
}
}

public List<Link> findEntities() {
return controller.findLinkEntities();
}

public void findEntity(int id) {
controller.findLink(id);
}

public int getCount() {
return controller.getLinkCount();
}

}

这些类中的方法相当重复。如果使用正确,我如何抽象通用类中的这些方法,例如:

package net.bounceme.dur.jpa.facades;

import java.util.List;
import java.util.logging.Logger;
import javax.persistence.EntityManager;

public abstract class AbstractFacade<T> {

private final static Logger log = Logger.getLogger(AbstractFacade.class.getName());
private final Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
}

public void edit(T entity) {
}

public void remove(T entity) {
}

public T find(Object id) {
}

public List<T> findAll() {
}

public List<T> findRange(int[] range) {
}

public int count() {
}

}

与此“外观”相关的相应JpaController:

package net.bounceme.dur.jpa.controllers;

import net.bounceme.dur.jpa.entities.Feed;
import net.bounceme.dur.jpa.entities.Link;
import net.bounceme.dur.jpa.exceptions.NonexistentEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

public class LinkJpaController implements Serializable {

public LinkJpaController(EntityManagerFactory emf) {
this.emf = emf;
}
private EntityManagerFactory emf = null;

public EntityManager getEntityManager() {
return emf.createEntityManager();
}

public void create(Link link) {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Feed feedId = link.getFeedId();
if (feedId != null) {
feedId = em.getReference(feedId.getClass(), feedId.getId());
link.setFeedId(feedId);
}
em.persist(link);
if (feedId != null) {
feedId.getLinkCollection().add(link);
feedId = em.merge(feedId);
}
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public void edit(Link link) throws NonexistentEntityException, Exception {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Link persistentLink = em.find(Link.class, link.getId());
Feed feedIdOld = persistentLink.getFeedId();
Feed feedIdNew = link.getFeedId();
if (feedIdNew != null) {
feedIdNew = em.getReference(feedIdNew.getClass(), feedIdNew.getId());
link.setFeedId(feedIdNew);
}
link = em.merge(link);
if (feedIdOld != null && !feedIdOld.equals(feedIdNew)) {
feedIdOld.getLinkCollection().remove(link);
feedIdOld = em.merge(feedIdOld);
}
if (feedIdNew != null && !feedIdNew.equals(feedIdOld)) {
feedIdNew.getLinkCollection().add(link);
feedIdNew = em.merge(feedIdNew);
}
em.getTransaction().commit();
} catch (Exception ex) {
String msg = ex.getLocalizedMessage();
if (msg == null || msg.length() == 0) {
Integer id = link.getId();
if (findLink(id) == null) {
throw new NonexistentEntityException("The link with id " + id + " no longer exists.");
}
}
throw ex;
} finally {
if (em != null) {
em.close();
}
}
}

public void destroy(Integer id) throws NonexistentEntityException {
EntityManager em = null;
try {
em = getEntityManager();
em.getTransaction().begin();
Link link;
try {
link = em.getReference(Link.class, id);
link.getId();
} catch (EntityNotFoundException enfe) {
throw new NonexistentEntityException("The link with id " + id + " no longer exists.", enfe);
}
Feed feedId = link.getFeedId();
if (feedId != null) {
feedId.getLinkCollection().remove(link);
feedId = em.merge(feedId);
}
em.remove(link);
em.getTransaction().commit();
} finally {
if (em != null) {
em.close();
}
}
}

public List<Link> findLinkEntities() {
return findLinkEntities(true, -1, -1);
}

public List<Link> findLinkEntities(int maxResults, int firstResult) {
return findLinkEntities(false, maxResults, firstResult);
}

private List<Link> findLinkEntities(boolean all, int maxResults, int firstResult) {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
cq.select(cq.from(Link.class));
Query q = em.createQuery(cq);
if (!all) {
q.setMaxResults(maxResults);
q.setFirstResult(firstResult);
}
return q.getResultList();
} finally {
em.close();
}
}

public Link findLink(Integer id) {
EntityManager em = getEntityManager();
try {
return em.find(Link.class, id);
} finally {
em.close();
}
}

public int getLinkCount() {
EntityManager em = getEntityManager();
try {
CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
Root<Link> rt = cq.from(Link.class);
cq.select(em.getCriteriaBuilder().count(rt));
Query q = em.createQuery(cq);
return ((Long) q.getSingleResult()).intValue();
} finally {
em.close();
}
}

}

混凝土立面应该是什么样子?

另请参阅:

https://stackoverflow.com/a/5636924/262852

http://en.wikipedia.org/wiki/Facade_pattern

最佳答案

您的假设是正确的,这些部分是重复的,因此是可以避免的。 Netbeans向您展示了一种实现方法,当您使用向导创建JSF Pages form Entity Classes时。结果如下所示:

AbstractFacade.java:

public abstract class AbstractFacade<T> {
private 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 T edit(T entity) {
return getEntityManager().merge(entity);
}

public void remove(T entity) {
getEntityManager().remove(getEntityManager().merge(entity));
}

// some more find helper methods ...
}

ConcreteEntityFacade.java:

public class ConcreteEntityFacade extends AbstractFacade<ConcreteEntity> {
@PersistenceContext
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
return em;
}

public ConcreteEntityFacade() {
super(ConcreteEntity.class);
}
}

希望对您有所帮助。

关于java - 来自抽象、通用外观的 JPA Controller 类的具体外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27028400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com