【发布时间】:2012-12-06 22:17:26
【问题描述】:
我正在尝试编写一个 JPQL 查询,该查询将删除所有由ArtContent ID 引用特定 ArtContent-s 的 PlaylistItem-s。
我试过了:
public int deleteItemsByContentIds(Long[] contentIds) {
EntityManager em = getEntityManager();
int result = em.createQuery(
"delete from PlaylistItem where artContent.id in (:idsArray) ")
.setParameter("idsArray", contentIds).executeUpdate();
return result;
}
但它会引发异常:
Servlet.service() for servlet RemoveContentServlet threw exception:
javax.ejb.EJBException: java.lang.IllegalArgumentException:
Encountered array-valued parameter binding, but was expecting [java.lang.Long]
什么是可以理解的,因为没有setParameter 方法将数组作为参数。那么解决此类问题的最佳方法是什么?
简化的类定义:
@Entity
public class PlaylistItem implements Serializable {
@Id
@GeneratedValue
private Long id;
private int position;
@ManyToOne(optional = false)
@JoinColumn(name = "PLAYLIST_ID")
private Playlist playlist;
@ManyToOne(optional = false)
@JoinColumn(name = "ART_CONTENT_ID")
private ArtContent artContent;
...
}
@Entity
public class ArtContent implements Serializable {
@Id
@GeneratedValue
private Long id;
...
}
【问题讨论】: