2014-02-28 43 views
1

我想執行一個hibernate查詢。之後,我創建的查詢, 「query.getQueryString()」 打印象下面這樣:休眠不能檢測命名參數

select a 
from com.mycompany.model.dwh.Instruction a 
where a.custBillAcctId = :accountId 

然後,當我試圖設置參數:

query.setParameter("accountId", new BigDecimal(accountId)); 

我得到:

org.hibernate.QueryParameterException: 
could not locate named parameter [accountId] 

我打印「query.getNamedParameters()」,它似乎是空的。 Hibernate有些無法檢測:accountId。我嘗試了不同的東西,通過參數索引設置等等都失敗了。 我在JEE-Hibernate中做過數百萬次,但是我用Spring-Hibernate失敗了。

環境:Eclipse的碼頭 休眠:4.1.9.Final springframework的:3.2.1.RELEASE


@Entity 
@Table(name = "TALIMAT") 
public class Instruction implements Serializable { 
private static final long serialVersionUID = 1L; 

@Id 
@Column(name = "TALIMAT_ID") 
@GenericGenerator(name="kaugen" , strategy="increment") 
@GeneratedValue(generator="kaugen") 
private Long key; 

@Id 
@Column(name = "CUST_BILL_ACCT_ID") 
private BigDecimal custBillAcctId; 

.... 

@Column(name = "STATUS") 
private String status; 

@Temporal(TemporalType.DATE) 
@Column(name = "INSERT_DATE") 
private Date insertDate; 

這裏是我的代碼:

try { 
     Session session = sessionFactory.getCurrentSession(); 

     Query query = session.createQuery("select a from " + 
           Instruction.class.getName() + " a 
           where a.custBillAcctId = :accountId "); 


     System.out.println("getNamedParameters: "); 
     for(String g:query.getNamedParameters()){ 
      System.out.println(g + "\n"); 
     } 

     query.setParameter("accountId", new BigDecimal(accountId)); 

    } catch (Exception e1) { 
     e1.printStackTrace(); 
    } 

感謝

+1

向我們顯示您的代碼。從查詢創建到執行。 –

+0

看看這些[相似1](http://stackoverflow.com/)(http://stackoverflow.com/questions/12433499/org-hibernate-queryparameterexception-could-not-locate-named-parameter)[Similar2](http://stackoverflow.com/問題/ 18680813/org-hibernate-queryparameterexception-could-not-locate-named-parameter-userid) –

+0

@Anirtak Varma我試過「類似1」但錯誤沒有改變 –

回答

2

我剛剛解決了這個問題。這是一個愚蠢的副作用問題。我有多個databaseContext.xml文件,每個文件都包含應用程序使用的不同數據庫的數據源定義。儘管它們在單獨的文件中,但數據源ID正好相反,這導致了模糊的行爲。

當我給他們獨特的名字,問題消失