JPQL 和实体 (java.lang.IllegalArgumentException)
JPQL and Entities (java.lang.IllegalArgumentException)
我正在创建一个 Web 应用程序,它需要 table 的通知来显示给不同的用户。出于某种原因,我编写的 JPQL 查询抛出 java.lang.IllegalArgumentException。我的应用程序已经有一个事务 table,使用相同的方法 (afaik) 进行结构化和查询,效果很好。
我一直在改组代码,更改变量名和字符大小写数小时,试图让它工作,但我每次仍然遇到异常。有谁知道我哪里出错了?
我的NotificationEntity如下:
@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
}
从使用以下方法实现接口 (NotificationStorageService) 的 EJB (NotificationStorageServiceBean) 调用 JPQL 查询:
@Override
public synchronized List<NotificationEntity> getUserNotificationList(String username)
{
List notifications;
notifications = em.createNamedQuery("fetch_user_notifications").setParameter("notificationrecipient", username).getResultList();
return notifications;
}
并且从我的 .xhtml UI 的 CDI 支持 bean 调用 EJB 方法,使用 FacesContext 的当前登录用户为这些方法提供参数。
@EJB
NotificationStorageService notificationStore;
public List<NotificationEntity> getUserNotificationList()
{
return notificationStore.getUserNotificationList(this.currentUser);
}
我得到的确切错误是:
java.lang.IllegalArgumentException:您试图使用查询字符串中不存在的通知接收者名称设置参数值 SELECT n FROM NotificationEntity n WHERE n.notificationrecipient=:username.
JPQL 查询中的参数名称以冒号开头。所以只需使用
setParameter("username", username)
我正在创建一个 Web 应用程序,它需要 table 的通知来显示给不同的用户。出于某种原因,我编写的 JPQL 查询抛出 java.lang.IllegalArgumentException。我的应用程序已经有一个事务 table,使用相同的方法 (afaik) 进行结构化和查询,效果很好。
我一直在改组代码,更改变量名和字符大小写数小时,试图让它工作,但我每次仍然遇到异常。有谁知道我哪里出错了?
我的NotificationEntity如下:
@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
}
从使用以下方法实现接口 (NotificationStorageService) 的 EJB (NotificationStorageServiceBean) 调用 JPQL 查询:
@Override
public synchronized List<NotificationEntity> getUserNotificationList(String username)
{
List notifications;
notifications = em.createNamedQuery("fetch_user_notifications").setParameter("notificationrecipient", username).getResultList();
return notifications;
}
并且从我的 .xhtml UI 的 CDI 支持 bean 调用 EJB 方法,使用 FacesContext 的当前登录用户为这些方法提供参数。
@EJB
NotificationStorageService notificationStore;
public List<NotificationEntity> getUserNotificationList()
{
return notificationStore.getUserNotificationList(this.currentUser);
}
我得到的确切错误是: java.lang.IllegalArgumentException:您试图使用查询字符串中不存在的通知接收者名称设置参数值 SELECT n FROM NotificationEntity n WHERE n.notificationrecipient=:username.
JPQL 查询中的参数名称以冒号开头。所以只需使用
setParameter("username", username)