簡體   English   中英

如何處理JPA命名查詢中數字類型的空值

[英]How to handle null value of number type in JPA named query

我想將兩個參數傳遞給namedquery。 一種是數字類型,另一種是字符串類型。 他們都可以為空。

例如,(id=null, username='joe') 和 (id=1, username='joe') 是兩個不同的結果。 在 namedQuery 中,如果 id 為 null,則語法為“u.id is null”,如果 id 不為 null,則語法為“u.id = :id”。 我的問題是如何動態處理namedQuery中提交的id?

請檢查我的示例代碼:

1.用戶.java

@NamedQueries({
        @NamedQuery(name = "getUser", query = "select u from User u"
                + " where u.id = :id"    
                + " And u.username= :username")
})
public class User{
     public Long id;
     public String username;
}
  1. 用戶道
public User getUser(Long id, String username) {
    TypedQuery<User> query = Dao.entityManager.createNamedQuery("getUser", User.class);
    query.setParameter("id", id);
    query.setParameter("username", username);

    List<User> users = query.getResultList();

    if (users.size() > 0) {
        return users.get(0);
    } else {
        return null;
    }
}

========================================

我嘗試過的:

  1. 這是遺留代碼,我不想更改結構。 所以我不想使用 Criteria。

  2. select u from User u where (:id is null or u.id= :id) and u.username= :username

    // 拋出異常:不一致的數據類型:預期 NUMBER 得到 BINARY

  3. select u from User u where u.id= nullif(:id, null) and u.username= :username

    // 拋出異常:不一致的數據類型:預期 NUMBER 得到 BINARY

  4. 我也在namedQuery中嘗試過nvl和decode,沒有用。

  5. query.setParameter("id", id==null?-1:id) // 無效。

我最后的選擇是在 UserDao 文件中編寫查詢來替換用戶文件中的 namedQuery。

謝謝 !

============================================

我的時間不多了,不得不放棄使用namedQuery。 我的解決方案:

# UserDao.java 

  public User getUser(Long id, String usename) {
        String getUser = "select u from user u where u.id " + Dao.isNull(id) 
                       + " And u.username " + Dao.isNull(username);
        Query query = Dao.entityManager.createQuery(getUser);
    }

# Dao.java

   public static String isNull(Object field) {
        if (field != null) {
                if (field instanceof String) {
                    return " = " + "'" + field + "'";
                } else {
                    return " = " + field;
                }

            } else {
                return " is NULL ";
            }
    }

您不能在運行時更改命名查詢。 這樣做會破壞命名查詢的目的。 應使用標准 API 創建動態查詢。

對在命名查詢使用什么答案。

   from CountryDTO c where 
    ((:status is null and c.status is null) or c.status = :status) 
    and c.type =:type

嘗試讓布爾值檢查空值。 它對我有用。

query.setParameter("idIsSet", id != null); 

query.setParameter("id", id); 

在命名查詢中:

(:idIsSet = false or id = :id)

當 id 為空時,“idIsSet”為假。 所以 ":idIsSet = false" 變為 true 並且不會檢查 id。

如果)在您的查詢末尾是一個錯誤,請嘗試不使用它

    @NamedQuery(name = "getUser", query = "select u from User u"
            + " where (:id is null or u.id = :id)"    
            + " And :username"

編輯

如果其他一切都失敗了,這個快速的解決方法應該結合上面的空檢查來完成(如果你在數據庫中沒有負 id)

query.setParameter("id", id == null ? -1 : id); 

檢出<=>運算符。 它檢查是否等於,還處理null。 在此處查看文檔https://dev.mysql.com/doc/refman/8.0/en/comparison-operators.html#operator_equal-to

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM