使用JPA JQL查询行EXIST

时间:2012-07-30 06:36:08

标签: java mysql jpa jpql

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方法转换为仅存在的查询?

1 个答案:

答案 0 :(得分:0)

您可以尝试简单查询:

SELECT 1 
FROM Newsgroups n 
WHERE newNewsgroup = 'newNewsgroup'
LIMIT 1;