0

甚至可以将泛型与genericPersistand一起使用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字段,确定该字段然后相应地查询数据库?

4

1 回答 1

1

嗯,我不太确定-您是否要替换isUniqueArticleand ?您可以在文章/新闻组中添加一些内容吗?如果您可以为它们添加一个接口和一个方法,那么可以。如果你不能那么可能你也可以这样做,但我想不出那样:)isUniqueNewsgroupisUniqueEntity

无论如何,你可以试试这个。为这两个类添加一个接口:

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(T other) (现在只需复制粘贴两个 isXUnique 中的“ifs”中的条件)。

如果那是您正在寻找并且有效的东西(尚未编译它!),persist 方法对您来说应该没问题:)

于 2012-07-31T07:32:23.020 回答