如何在Hibernate中删除子条目的id ?

时间:2022-11-25 12:12:44

I'm trying to write an JPQL query which would delete all PlaylistItem-s refering to specific ArtContent-s by ArtContent IDs.

我正在尝试编写一个JPQL查询,该查询将删除所有由ArtContent id引用的特定ArtContent-s的所有playlistiem -s。

I tried this:

我试着这样的:

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;
}

but it throws an exception:

但它引发了一个例外:

Servlet.service() for servlet RemoveContentServlet threw exception: 
javax.ejb.EJBException: java.lang.IllegalArgumentException: 
Encountered array-valued parameter binding, but was expecting [java.lang.Long]

What is understandable, because there is no setParameter method taking an array as an argument. So what is the best way to solve such an issue?

什么是可以理解的,因为没有setParameter方法以数组作为参数。那么解决这类问题的最好方法是什么呢?

Simplified class definition:

简化的类定义:

@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;
...
}

2 个解决方案

#1


7  

You can keep using .setParameter but you need to make the value extend a Collection (like an ArrayList) instead of using an array type. Maybe just change to:

您可以继续使用. setparameter,但是您需要使该值扩展一个集合(像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;
}

#2


1  

Try setParameterList instead.

尝试setParameterList代替。

Edit:

编辑:

Sorry, for JPA convert it to a Collection (Arrays.asList(arrayOfLongs)) and just use setParameter.

抱歉,JPA将其转换为集合(Arrays.asList(arrayOfLongs)),并只使用setParameter。

Edit 2: Beaten to the punch!

编辑2:挨打!

#1


7  

You can keep using .setParameter but you need to make the value extend a Collection (like an ArrayList) instead of using an array type. Maybe just change to:

您可以继续使用. setparameter,但是您需要使该值扩展一个集合(像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;
}

#2


1  

Try setParameterList instead.

尝试setParameterList代替。

Edit:

编辑:

Sorry, for JPA convert it to a Collection (Arrays.asList(arrayOfLongs)) and just use setParameter.

抱歉,JPA将其转换为集合(Arrays.asList(arrayOfLongs)),并只使用setParameter。

Edit 2: Beaten to the punch!

编辑2:挨打!