Apache OpenJPA - NamedQuery异常

时间:2012-03-06 14:07:09

标签: java exception jpa-2.0 openjpa

我有两个表与manyToMany关系:

@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>();

        .......
}

不,我想找到一个带有NamedQuerd arelation.findA的ABean,如下所示:

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

我使用数据库中的持久对象设置bList。

但是当我想找到“ABean”时,我会得到以下异常:

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"

有人知道我为什么会遇到此例外情况吗? 我只想知道,如果具有此名称的ABean和此BBeans存在于DB中。

编辑: 好的,我知道原因: 参数应该是BBean而不是它的List。 但是我如何使用BBeans列表?

最诚挚的问候 Veote

1 个答案:

答案 0 :(得分:1)

在命名查询中,您应该在尝试引用表时使用类名,尝试使用此

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

,第二个是你不需要创建新对象, 让它留下List<BBean> aList;

希望这会对你有所帮助。