veote
veote

Reputation: 1480

Apache OpenJPA - NamedQuery Exception

I hava two tables with a manyToMany relation :

@Entity(name = "arelation")
@NamedQueries({ @NamedQuery(name = "arelation.findAByName", query = "SELECT a FROM arelation a WHERE a.arelationname = :aname"),
    @NamedQuery(name = "arelation.findA", query = "SELECT a FROM arelation a WHERE a.arelationname = :aname and a.bList = :bList")
})
public class ABean{

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @NotNull
    private String arelationname;

    @ManyToMany(targetEntity = BBean.class, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JoinTable(name = "joinAB", joinColumns = { @JoinColumn(name = "aID") }, inverseJoinColumns = { @JoinColumn(name = "bID") })
    private List<BBean> bList= new ArrayList<BBean>();

        .......
}

@Entity(name = "brelation")
@NamedQueries({ @NamedQuery(name = "brelation.findB", query = "SELECT b FROM brelation b WHERE b.brelationname = :bname)})
public class BBean{
    /*
     * private Bean Variables
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    @NotNull
    private String brelationname;


    @ManyToMany(mappedBy = "bList")
    @NotNull
    private List<ABean> aList= new ArrayList<ABean>();

        .......
}

No I want to find a ABean with the NamedQuerd arelation.findA like this:

public ABean findABean(EntityManager em, ABean a)
        throws NoResultException {
    return  (ABean) em.createNamedQuery("arelation.findA")
            .setParameter("aname", a.getArelationname())
            .setParameter("blist", a..getBList())
            .getSingleResult();
}

I set the bList with persisted objects from the database.

But when I want to find the "ABean" i get following Exception:

java.lang.IllegalArgumentException: Parameter "Parameter<BBean>('blist')" declared in "SELECT a FROM arelation a WHERE a.arelationname = :aname and a.bList = :bList" is set to value of "[     ArelationName: name0
,       ArelationName: name1
,       ArelationName: name2
,       ArelationName: name3
,       ArelationName: name4
,       ArelationName: name5
,       ArelationName: name6
,       ArelationName: name7
]" of type "org.apache.openjpa.util.java$util$ArrayList$proxy", but this parameter is bound to a field of type "mypackage.BBean"

Does someone know why I get this Exception? I only want to know, if the ABean with this name and this BBeans exists in the DB.

EDIT: Ok, I know the reason: The Parameter should be a BBean and not a List of it. But how can I use a List of BBeans?

Best Regards Veote

Upvotes: 0

Views: 1379

Answers (1)

ilya.stmn
ilya.stmn

Reputation: 1624

In named query you should use class name when trying to refer to table, try to use this

@NamedQueries({ @NamedQuery(name = "brelation.findB", query = "SELECT b FROM BBean b WHERE b.brelationname = :bname)})

and the second one is that you do not need to create new object , just let it stay List<BBean> aList;

hope this will help you.

Upvotes: 1

Related Questions