Ank*_*nke 8 hibernate jpa jpql
我正在尝试编写一个JPQL查询,它会删除所有PlaylistItem-s,ArtContent通过ArtContentID 来引用特定的-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;
}
Run Code Online (Sandbox Code Playgroud)
但它引发了一个例外:
Servlet.service() for servlet RemoveContentServlet threw exception:
javax.ejb.EJBException: java.lang.IllegalArgumentException:
Encountered array-valued parameter binding, but was expecting [java.lang.Long]
Run Code Online (Sandbox Code Playgroud)
什么是可以理解的,因为没有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;
...
Run Code Online (Sandbox Code Playgroud)
}
@Entity
public class ArtContent implements Serializable {
@Id
@GeneratedValue
private Long id;
...
}
Run Code Online (Sandbox Code Playgroud)
您可以继续使用,.setParameter但需要使值扩展Collection(如ArrayList)而不是使用数组类型.也许只需改为:
public int deleteItemsByContentIds(Long[] contentIds) {
EntityManager em = getEntityManager();
int result = em.createQuery(
"delete from PlaylistItem where artContent.id in (:idsArray) ")
.setParameter("idsArray", Arrays.asList(contentIds)).executeUpdate();
return result;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2528 次 |
| 最近记录: |