1

I've set a NamedQuery like this:

@Entity
@Table(name = "channel")
@NamedQuery(name = "Channel.getPrivateChannel", query = "SELECT pvt from Channel pvt WHERE pvt.state = 3 AND pvt.channelOwnerWorkspace = :channelOwnerWorkspace AND pvt.channelSubscribers = :channelSubscribers")
public class Channel implements Serializable {

...

@Column(nullable = false)
    private int type;

@ManyToOne
    @JoinColumn(name = "workspaceId")
    private Workspace channelOwnerWorkspace;

@ManyToMany(mappedBy = "subscribedChannels", fetch = FetchType.EAGER)
    private Set<User> channelSubscribers;

All the variables are non null and exist; and, when calling said Query, I get an IllegalArgumentException like this:

Parameter value [pt.project.entity.User@4125ce40] did not match expected type [java.util.Set (n/a)]

The exception is thrown here:

public Channel findPrivateChannel1(String workspace, Set<User> channelSubscribers) {


///

        Workspace selectedWorkspace = workspaceDAO.findByTitle(workspace);

        try {
            Channel pvtChannel = em.createNamedQuery("Channel.getPrivateChannel", Channel.class)
                    .setParameter("channelOwnerWorkspace", selectedWorkspace)
                    .setParameter("channelSubscribers", channelSubscribers).getSingleResult();

What am I doing wrong? Is it even possible to set an HashSet as parameter?

Thanks in advance.

plmkr
  • 11
  • 2
  • What are you passing when calling this namedQuery ? If you can share the code where you are calling this named Query and getting above exception. – Sariq Shaikh Apr 23 '20 at 06:49
  • Sure. The exception is throw here: `public Channel findPrivateChannel1(String workspace, Set channelSubscribers) { /// try { Channel pvtChannel = em.createNamedQuery("Channel.getPrivateChannel", Channel.class) .setParameter("channelOwnerWorkspace", selectedWorkspace) .setParameter("channelSubscribers", channelSubscribers).getSingleResult(); return pvtChannel; } catch (NoResultException e) { return null; } catch (Exception e) { return null; } ` Resulting in the second exception. – plmkr Apr 23 '20 at 16:46

1 Answers1

1

The equals comparison operator, accordingly to JSR-338, item 4.6.7, cannot be used with an entire collection:

On a side note, it is not clear what is your goal in using a comparison of two entire lists. More specifically, you could use some very useful collection expressions to select your entities, such as IN, MEMBER OF or IS EMPTY, or even some subselects (ANY, SOME or ALL).

Paulo Araújo
  • 539
  • 3
  • 12
  • The query should have been: `"@NamedQuery(name = "Channel.getPrivateChannel", query = "SELECT pvt from Channel pvt WHERE pvt.state = 3 AND pvt.channelOwnerWorkspace = :channelOwnerWorkspace AND pvt.channelSubscribers IN :channelSubscribers")";` nonetheless the point was to find a Channel where there were only and only the Users on said Set. My problem is NamedQuery doesn't uses the entire Set, only one of the User. – plmkr Apr 23 '20 at 16:45