【问题标题】:JPQL: set a column to null in an updateJPQL:在更新中将列设置为空
【发布时间】:2012-10-23 20:59:53
【问题描述】:

以下JPQL:

UPDATE SignIn signIn SET signIn.cookieUUID = null WHERE signIn.user.id = :userID

由于“= null”而给我以下错误:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IS NULL WHERE `B0`.`ID` = 9' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

但是当我将该字段设置为空字符串 (= '') 时它会起作用。

那么如何使用 JQPL 将列设置为空?

【问题讨论】:

  • 这里有一个similar post(用于 HQL)。它应该可以帮助您解决它。希望对您有所帮助。
  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review

标签: java jpa jpql datanucleus


【解决方案1】:

我认为是NULL(大写)::参考JPQLEclipselink

 UPDATE SignIn signIn SET signIn.cookieUUID = NULL WHERE signIn.user.id = :userID

或者使用参数查询:

     String sQuery = "UPDATE SignIn signIn SET signIn.cookieUUID = :cookieUUID "+
                 "WHERE signIn.user.id = :userID";
     Query query= entityManager.createQuery(sQuery );
     query.setParameter("cookieUUID", null);
     query.setParameter("userID", userID);
     query.executeUpdate();

【讨论】:

  • 这仍然给了我同样的错误(我在询问之前也尝试过);)
  • 该字段只是注解@Column,隐含表示该列可以为空。
  • @AndrewBourgeois:您可以使用参数查询吗?我尝试使用 EclipseLink,似乎可以正常工作。
  • 我知道它是这样工作的,但是当它真的是静态的时,我发现这样做真的很尴尬。不过,感谢您的所有研究!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-29
  • 2016-08-22
  • 1970-01-01
相关资源
最近更新 更多