0

isUnique应该只检查新闻组的存在并返回一个反映以下内容的布尔值:

init:
Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
deps-jar:
Updating property file: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties
compile:
run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 5 groups in 26ms
Jul 29, 2012 8:37:13 PM net.bounceme.dur.usenet.driver.Main getFolders
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
[EL Info]: 2012-07-29 20:37:17.607--ServerSession(20908501)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-07-29 20:37:19.815--ServerSession(20908501)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main persist
INFO: gwene.com.androidcentral
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: []
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: gwene.com.androidcentral  new
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main persist
INFO: gwene.com.blogspot.emacsworld
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: [gwene.com.androidcentral]
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: gwene.com.blogspot.emacsworld new
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main persist
INFO: gwene.com.blogspot.googlecode
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld]
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: gwene.com.blogspot.googlecode new
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main persist
INFO: gwene.com.blogspot.googlereader
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode]
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: gwene.com.blogspot.googlereader   new
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main persist
INFO: gwene.com.economist
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader]
Jul 29, 2012 8:37:20 PM net.bounceme.dur.usenet.driver.Main isUnique
INFO: gwene.com.economist   new
BUILD SUCCESSFUL (total time: 9 seconds)

通过查询数据库,这似乎像宣传的那样工作。但是,这似乎是错误的查询类型:

package net.bounceme.dur.usenet.driver;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import net.bounceme.dur.usenet.model.Newsgroups;
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) {
        Main main = new Main();
    }

    public Main() {
        List<Newsgroups> folderGroups = getFolders();
        EntityManagerFactory emf;
        EntityManager em;
        emf = Persistence.createEntityManagerFactory("USENETPU");
        em = emf.createEntityManager();
        for (Newsgroups n : folderGroups) {
            persist(em, n);
        }
        em.close();
    }

    private void persist(EntityManager em, Newsgroups newNewsgroup) {
        LOG.info(newNewsgroup.toString());
        TypedQuery<Newsgroups> query = em.createQuery("SELECT n FROM Newsgroups n", Newsgroups.class);
        List<Newsgroups> results = query.getResultList();
        if (isUnique(newNewsgroup, results)) {
            em.getTransaction().begin();
            em.persist(newNewsgroup);
            em.getTransaction().commit();
        }
    }

    private boolean isUnique(Newsgroups newNewsgroup, Iterable<Newsgroups> results) {
        LOG.info(results.toString());
        for (Newsgroups existingNewsgroup : results) {
            if ((existingNewsgroup.getNewsgroup().equals(newNewsgroup.getNewsgroup()))) {
                return false;
            }
        }
        LOG.info(newNewsgroup + "\tnew");
        return true;
    }

    private boolean queryDB2(EntityManager em, Newsgroups ng) {
        String newsgroup = ng.getNewsgroup();
        TypedQuery<Newsgroups> query = em.createQuery("SELECT n FROM Newsgroups n WHERE n.newsgroup = :newsgroup", Newsgroups.class);
        Newsgroups result = query.getSingleResult();
        LOG.info("query result:\n" + result + "\ncompared to\n" + ng);
        return false;
    }

    private List<Newsgroups> getFolders() {
        List<Folder> folders = u.getFolders();
        List<Newsgroups> newsgroups = new ArrayList<>();
        for (Folder folder : folders) {
            Newsgroups newsgroup = new Newsgroups(folder);
            newsgroups.add(newsgroup);
        }
        LOG.info(newsgroups.toString());
        return newsgroups;
    }
}

在哪里而不是使用isUnique来遍历查询结果,匹配的语法是什么?

我看到在SQL中有一个EXISTS关键词。我怎样才能把这个isUnique方法变成一个存在的查询?

4

1 回答 1

0

您可以尝试简单的查询:

SELECT 1 
FROM Newsgroups n 
WHERE newNewsgroup = 'newNewsgroup'
LIMIT 1;
于 2012-07-30T06:51:34.523 回答