2014-01-24 66 views
1

當試圖通過JPA來檢索數據庫中的表,並填寫到表SWT窗口小部件(org.eclipse.swt.widgets.Table),我在執行下面的Java代碼得到一個轉換錯誤:字符串搜索運行時錯誤

String SQL = String.format("select t.id, t.one, t.two from Test t where t.one like '%%s%' order by t.id",one); //variable one is defined as a parameter of the method (whole method not included here for simplicity) 

//this is the code that uses the SQL variable above 
EntityManager connection = DB.Connection().createEntityManager(); //the EntityManagerFactory 
TypedQuery<Object[]> query = connection.createQuery(SQL, Object[].class); /*gives the same as cast (TypedQuery<Object[]>) */ 
List<Object[]> tablelist = query.getResultList(); 
connection.close(); 

SWTtable.removeAll(); //clears all the elements in the table 

for (Object[] row : tablelist) { 
    final TableItem item = new TableItem(SWTtable, SWT.None); 
    item.setData(row[0]); 
    item.setText(0, row[1].toString()); 
} 

錯誤java.util.UnknownFormatConversionException: Conversion = '''上運行時用於線路String SQL = String.format("select t.id, t.one, t.two from Test p where p.one like '%%s%' order by p.id",one);

問題接縫是在String SQL可變'%%s%'。我需要保留'%%s%',以便搜索使用上面的查詢選擇的全部或一個記錄。

  • '%%' - 返回所有記錄,但沒有字符串參數
  • '%s' - 給出了一個具體的記錄,如果在SQL查詢語句是 正確

其他的一切給出了一個問題,該字符串'%% s%'不是我寫的,但我需要保留它或通過保持相同的結果來改變它。我想要的結果是給所有記錄,但在收到String參數後返回該特定記錄。

這裏有什麼問題超出了我的JPA知識範圍。

+1

您可能需要使用'NameQuery'像'「選擇t.id ,t.one,t.two from Test t where t.one like:one order by t.id' and then'namedQuery.setParameter(「one」,「%」+ s +「%」)'; –

+0

NameQuery我可以嘗試,但一個setParameter方法只返回一個由該參數指定的特定記錄 –

+0

setParameter只是將參數設置爲查詢類似'PreparedStatement'中的'?'。看看我的答案,我們有':one'在查詢中作爲一個命名參數。 –

回答

2

我剛剛檢查了TypedQuery API,你可以設置它的參數。

TypedQuery<Object[]> query = connection.createQuery("select t.id, t.one, t.two from Test t where t.one like :one order by t.id", Object[].class); 
query.setParamter("one", "%" + s + "%"); 
List<Object[]> tablelist = query.getResultList(); 

更新:

好像這兩個條件似乎分離像其他的回答表明,你可以通過生成查詢類似

StringBuilder query = new StringBuilder("select t.id, t.one, t.two from Test t "); 
if (StringUtils.isNotEmpty(s)) { 
    query.append(" where t.one like :one "); 
} 
query.append(" order by t.id"); 
TypedQuery<Object[]> query = connection.createQuery(query.toString()); 
+0

嘗試了它並給出完全相同的結果,我甚至沒有添加參數,我只能在查詢中提供「%s%」。每次搜索後,我現在必須提供一個額外的空間字符以使其工作,因爲使用了最後一個%。 –

+0

上面的查詢給出了所有在'one'列具有's'的行。你能否告訴我們什麼樣的價值觀可以成爲你想要的結果? –

+0

好像它說s是字符串。我提供了一個字符串,即「one」,等於表Test中的「one」屬性。由於我還想檢索未提供字符串「one」的其餘字符,因此我將%放在需要它的位置以獲取表的所有屬性Test並在完成參數s時返回特定的屬性名稱在查詢中提供。希望很明顯。 –

0

做個人而言,我會讓這兩個查詢它會表現更好。事情是這樣的:

public List<Object[]> collectOne(String param) { 
    TypedQuery<Object[]> query = connection.createQuery("select t.id, t.one, t.two from Test t where t.one like :one order by t.id", Object[].class); 
    query.setParamter("one", "%" + param + "%"); 
    return query.getResultList(); 
} 

public List<Object[]> collectAll() { 
    TypedQuery<Object[]> query = connection.createQuery("select t.id, t.one, t.two from Test t order by t.id", Object[].class); 
    return query.getResultList(); 
} 

那麼你可以隨時有可能會或可能不帶任何參數的怪異方法:

public List<Object[]> collectOneOrAll(String param) { 
    if (param == null || param.isEmpty()) 
     return collectAll(); 

    return collectOne(param); 
} 
+0

收集所有不需要的東西,因爲當我想要檢索所有內容時,我已經分開了,但事實並非如此。這是收集所有和字符串特定字或字母的組合。當我提供時,它需要檢索一個或多個屬性的其餘字母。 s只能是一個字母,但它也可以是一個字。該單詞在該查詢中由%完成。唯一我無法弄清楚的是,錯誤是如何產生的,以及它的意思。在JPA/hibernate文檔中沒有提及它。 –

+0

你不清楚。您需要爲我們提供失敗的測試用例的代碼以及您正在獲取的異常的堆棧跟蹤。 –

+0

已解決!現在不需要。 –