3

以下のクエリの引数のパラメーターを設定するにはどうすればよいですか。

実在物:

package net.bounceme.dur.usenet.model;

import java.io.Serializable;
import java.util.logging.Logger;
import javax.mail.Folder;
import javax.mail.Message;
import javax.persistence.*;
import net.bounceme.dur.usenet.driver.FetchBean;

@Entity
public class Article implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(Article.class.getName());
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Column
    private int messageNumber;
    @ManyToOne(cascade = CascadeType.PERSIST)
    private Newsgroup newsgroup;

    public Article() {
    }

    public Article(Message message, Folder folder) {
        messageNumber = message.getMessageNumber();
        EntityManagerFactory emf;
        EntityManager em;
        emf = Persistence.createEntityManagerFactory("USENETPU");
        em = emf.createEntityManager();
        String ng = folder.getFullName();
        Query query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :newsgroup", Newsgroup.class);
        Newsgroup result = (Newsgroup) query.getSingleResult();
        newsgroup = (result == null) ? new Newsgroup(folder) : result;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Article)) {
            return false;
        }
        Article other = (Article) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "\nmessageNumber\t" + messageNumber;
    }

    public int getMessageNumber() {
        return messageNumber;
    }

    public void setMessageNumber(int messageNumber) {
        this.messageNumber = messageNumber;
    }
}

スタックトレース:

run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 5 groups in 39ms
[EL Info]: 2012-08-03 03:26:30.737--ServerSession(13970219)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-08-03 03:26:31.815--ServerSession(13970219)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
Aug 03, 2012 3:26:32 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
Aug 03, 2012 3:26:32 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: Google helping out newcomers with Nexus 7 getting started video
Aug 03, 2012 3:26:33 AM net.bounceme.dur.usenet.driver.FetchBean main
SEVERE: null
java.lang.IllegalStateException: Query argument newsgroup not found in the list of parameters provided during query execution.
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.processParameters(EJBQueryImpl.java:829)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.executeReadQuery(EJBQueryImpl.java:406)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:773)
    at net.bounceme.dur.usenet.model.Article.<init>(Article.java:34)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:41)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:21)

BUILD SUCCESSFUL (total time: 13 seconds)

ニュースグループ名の名前付きパラメーターは , である必要がありますが、フィールドng内のその文字列を照会するための構文を取得できないようです。Newsgroup.newsgroupクエリを修正するにはどうすればよいですか?

- - - - - - -編集 - - - - - - - -

新しいトレース:

run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 5 groups in 30ms
[EL Info]: 2012-08-03 03:57:03.647--ServerSession(33288487)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-08-03 03:57:04.691--ServerSession(33288487)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
Aug 03, 2012 3:57:05 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
Aug 03, 2012 3:57:05 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: Google helping out newcomers with Nexus 7 getting started video
Aug 03, 2012 3:57:06 AM net.bounceme.dur.usenet.driver.FetchBean main
SEVERE: null
java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of gwene.com.androidcentral that does not exist in the query string SELECT n FROM Newsgroup n WHERE n.newsgroup = :ng.
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameterInternal(EJBQueryImpl.java:1256)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:1138)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.setParameter(EJBQueryImpl.java:81)
    at net.bounceme.dur.usenet.model.Article.<init>(Article.java:34)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:41)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:21)

BUILD SUCCESSFUL (total time: 17 seconds)

コードスニペット:

String ng = folder.getFullName();
        Query query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :ng", Newsgroup.class);
        query.setParameter(ng, ng);   //newsgroup object????
        Newsgroup result = (Newsgroup) query.getSingleResult();
        LOG.info(result.toString());
        newsgroup = (result == null) ? new Newsgroup(folder) : result;

私はそれをいじっていて、間違いなくそれを手に入れるでしょう。ドキュメントAPIをもう少し読む必要がありますが、それは十分に近いです:)

4

3 に答える 3

3
Query query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :newsgroup");
query.setParameter("newsgroup",newsgroup object);
Newsgroup result = (Newsgroup) query.getSingleResult();
于 2012-08-03T10:47:05.480 に答える
0

または、クエリの setString(..) メソッドを使用できます。

Query query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :newsgroup", Newsgroup.class);
query.setString("newsgroup", ng);
Newsgroup result = (Newsgroup) query.getSingleResult();
于 2012-08-03T10:49:57.363 に答える
0

記録のために、以下が実行されます。

public Article(Message message, Folder folder) {
    messageNumber = message.getMessageNumber();
    EntityManagerFactory emf;
    EntityManager em;
    emf = Persistence.createEntityManagerFactory("USENETPU");
    em = emf.createEntityManager();
    String ng = folder.getFullName();
    TypedQuery<Newsgroup> query = em.createQuery("SELECT n FROM Newsgroup n WHERE n.newsgroup = :newsgroup", Newsgroup.class);
    query.setParameter("newsgroup", ng);//newsgroup object
    Newsgroup result = query.getSingleResult();
    LOG.info(result.toString());
    newsgroup = (result == null) ? new Newsgroup(folder) : result;
}

次の軽微なエラーのみ:

run:
DEBUG: nntp: newsrc loading /home/thufir/.newsrc
DEBUG: nntp: newsrc load: 5 groups in 12ms
[EL Info]: 2012-08-03 04:21:28.427--ServerSession(18852961)--EclipseLink, version: Eclipse Persistence Services - 2.3.0.v20110604-r9504
[EL Info]: 2012-08-03 04:21:29.496--ServerSession(18852961)--file:/home/thufir/NetBeansProjects/USENET/build/classes/_USENETPU login successful
Aug 03, 2012 4:21:30 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: [gwene.com.androidcentral, gwene.com.blogspot.emacsworld, gwene.com.blogspot.googlecode, gwene.com.blogspot.googlereader, gwene.com.economist]
Aug 03, 2012 4:21:30 AM net.bounceme.dur.usenet.driver.FetchBean <init>
INFO: Google helping out newcomers with Nexus 7 getting started video
Aug 03, 2012 4:21:31 AM net.bounceme.dur.usenet.driver.FetchBean main
SEVERE: null
javax.persistence.NoResultException: getSingleResult() did not retrieve any entities.
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.throwNoResultException(EJBQueryImpl.java:1310)
    at org.eclipse.persistence.internal.jpa.EJBQueryImpl.getSingleResult(EJBQueryImpl.java:778)
    at net.bounceme.dur.usenet.model.Article.<init>(Article.java:35)
    at net.bounceme.dur.usenet.driver.FetchBean.<init>(FetchBean.java:41)
    at net.bounceme.dur.usenet.driver.FetchBean.main(FetchBean.java:21)

BUILD SUCCESSFUL (total time: 14 seconds)

チュートリアルはあまり役に立ちませんでしたが、少なくともその構文の問題は修正されたと思います。

于 2012-08-03T11:24:47.320 に答える