【发布时间】:2012-03-06 14:07:09
【问题描述】:
我有两个具有多对多关系的表:
@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是否存在于数据库中。
编辑: 好的,我知道原因了: Parameter 应该是一个 BBean 而不是它的 List。 但是如何使用 BBean 列表?
最好的问候 投票
【问题讨论】:
标签: java exception jpa-2.0 openjpa