甚至可以将泛型与genericPersist
和isUniqueEntity
一起使用吗? persist方法似乎很简单:
package net.bounceme.dur.usenet.driver;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.mail.Message;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import net.bounceme.dur.usenet.model.Article;
import net.bounceme.dur.usenet.model.Newsgroup;
import net.bounceme.dur.usenet.model.Usenet;
public class Main {
private static final Logger LOG = Logger.getLogger(Main.class.getName());
private Usenet u = Usenet.INSTANCE;
public static void main(String[] args) {
try {
Main main = new Main();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Main() throws Exception {
EntityManagerFactory emf;
EntityManager em;
emf = Persistence.createEntityManagerFactory("USENETPU");
List<Newsgroup> subscribed = getFolders();
em = emf.createEntityManager();
for (Newsgroup newsgroup : subscribed) {
persistNewsgroups(em, newsgroup);
List<Message> messages = u.getMessages(newsgroup.getNewsgroup());
LOG.fine(newsgroup + " " + messages.size() + " messages");
for (Message message : messages) {
LOG.fine("message " + message.getMessageNumber());
Article article = new Article(message);
persistArticle(em, article);
}
}
em.close();
}
private boolean isUniqueArticle(Article article, List<Article> articles) {
LOG.fine(articles.toString());
for (Article a : articles) {
if (a.getSubject().equalsIgnoreCase(article.getSubject())) {
return false;
}
}
LOG.fine("new\t\t" + article);
return true;
}
private void persistArticle(EntityManager em, Article article) {
LOG.fine(article.toString());
TypedQuery<Article> query = em.createQuery("SELECT a FROM Article a", Article.class);
List<Article> results = query.getResultList();
if (isUniqueArticle(article, results)) {
em.getTransaction().begin();
em.persist(article);
em.getTransaction().commit();
}
}
private <T> void genericPersist(EntityManager em, Class<T> entity, String queryString) {
TypedQuery<T> query = em.createQuery(queryString, entity);
List<T> results = query.getResultList();
if (isUniqueEntity(entity, results)) {
em.getTransaction().begin();
em.persist(entity);
em.getTransaction().commit();
}
}
private <T> boolean isUniqueEntity(Class<T> entity,List<T> results) {
return false;
}
private void persistNewsgroups(EntityManager em, Newsgroup newNewsgroup) {
LOG.fine(newNewsgroup.toString());
TypedQuery<Newsgroup> query = em.createQuery("SELECT n FROM Newsgroup n", Newsgroup.class);
List<Newsgroup> results = query.getResultList();
if (isUniqueNewsgroup(newNewsgroup, results)) {
em.getTransaction().begin();
em.persist(newNewsgroup);
em.getTransaction().commit();
}
}
private boolean isUniqueNewsgroup(Newsgroup newNewsgroup, Iterable<Newsgroup> results) {
LOG.fine(results.toString());
for (Newsgroup existingNewsgroup : results) {
if ((existingNewsgroup.getNewsgroup().equals(newNewsgroup.getNewsgroup()))) {
return false;
}
}
LOG.fine(newNewsgroup + "\tnew");
return true;
}
private List<Newsgroup> getFolders() {
List<Folder> folders = u.getFolders();
List<Newsgroup> newsgroups = new ArrayList<>();
for (Folder folder : folders) {
Newsgroup newsgroup = new Newsgroup(folder);
newsgroups.add(newsgroup);
}
LOG.fine(newsgroups.toString());
return newsgroups;
}
}
对于
isUniqueEntity
,我能想到的就是确定对象的类型,然后使用开关,但这似乎并没有太大的节省。如何才能做到这一点?假设实体具有单个
@Unique
字段,确定该字段,然后相应地查询数据库? 最佳答案
嗯,我不太确定-您是否要用isUniqueArticle
替换isUniqueNewsgroup
和isUniqueEntity
?您可以向Article / NewsGroup添加一些内容吗?如果可以为它们添加接口,并为每个方法添加方法,则可以。如果不能,那么您也可以做到,但我想不到:)
无论如何,您可以尝试一下。将接口添加到两个类:
public interface Equalable<T> {
boolean isEqual(T other);
}
private <T extends Equalable> boolean isUniqueEntity(T entity, Iterable<T> results) {
LOG.fine(results.toString());
for (T resultEntity : results) {
if (resultEntity.isEqual(entity))) {
return false;
}
}
LOG.fine(newNewsgroup + "\tnew");
return true;
}
然后在两个类中都实现isEqual(Tother)(现在只需在两个isXUnique中复制粘贴您在“ ifs”中的条件)。
如果那是您想要的并且可以正常工作(尚未编译!),那么persist方法对您来说应该没有问题:)