Question

I am trying to execute a hibernate query. After I created the query, "query.getQueryString()" prints like below:

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

Then when I am trying to set parameter:

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

I get:

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

I print "query.getNamedParameters()", it seems empty. Hibernate somewhat can't detect :accountId. I tried different things, setting by parameter index, etc. All failed. I did it millions of times in JEE-Hibernate, but I failed with Spring-Hibernate.

Environment: Eclipse-Jetty hibernate: 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;

Here is my code:

    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();
    }

Thanks

Was it helpful?

Solution

I just solved the problem. It was a silly side-effect problem. I have multiple databaseContext.xml files each containing a datasources definition for a different database which application use. Although they are in seperate files, datasource id's were coinciding, this resulted in ambiguous behaviour.

When I gave them unique names, problem disappeared

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top