Search code examples
glassfishejbjpql

JPQL and Entities (java.lang.IllegalArgumentException)


I am creating a web application that needs a table of notifications to display to various users. For some reason the JPQL query I've written is throwing a java.lang.IllegalArgumentException. My application already has a transaction table, structured and queried using an identical approach (afaik), which works perfectly.

I have been shuffling the code around, changing variable names and character cases for hours trying to get this to work but I'm still getting the exception every time. Does anyone have any idea where I'm going wrong?

My NotificationEntity is as follows:

    @Table(name="notificationentity")
    @NamedQuery(name="fetch_user_notifications", query="SELECT n FROM NotificationEntity n WHERE n.notificationrecipient=:username")
    @Entity
    public class NotificationEntity implements Serializable
    {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;

      @NotNull
      String notificationSender;

      @NotNull
      String notificationrecipient;

      ... other fields + methods
    }

The JPQL query is called from an EJB (NotificationStorageServiceBean) that implements an interface (NotificationStorageService) with the following method:

    @Override
    public synchronized List<NotificationEntity> getUserNotificationList(String username)
    {
      List notifications;
      notifications = em.createNamedQuery("fetch_user_notifications").setParameter("notificationrecipient", username).getResultList();
      return notifications;
    }

And the EJB method is called from a CDI backing bean for my .xhtml UI, using the FacesContext's currently logged in user to provide the argument for these methods.

    @EJB
    NotificationStorageService notificationStore;

    public List<NotificationEntity> getUserNotificationList()
    {
      return notificationStore.getUserNotificationList(this.currentUser);
    }

The exact error I get is: java.lang.IllegalArgumentException: You have attempted to set a parameter value using a name of notificationrecipient that does not exist in the query string SELECT n FROM NotificationEntity n WHERE n.notificationrecipient=:username.


Solution

  • The parameter name in a JPQL query starts with a colon. So just use

    setParameter("username", username)