spring - Issue with writing named sql query with hibernate -
i trying access database fk using named sql query hibernate, idea query customer table contains name, , companyid,etc. companyid fk commpany table. query wrote follows:
@namednativequery(name="getcustomer", query="select customer.* customer,company customer_first_name = (?1) , customer_last_name= (?2) , customer_company_id_fk = (?3) ",resultclass=customer.class)
the issue having follow:
exception in thread "main" org.hibernate.queryparameterexception: position beyond number of declared ordinal parameters. remember ordinal parameters 1-based! position: 2 @ org.hibernate.engine.query.spi.parametermetadata.getordinalparameterdescriptor(parametermetadata.java:89) @ org.hibernate.engine.query.spi.parametermetadata.getordinalparameterexpectedtype(parametermetadata.java:109) @ org.hibernate.internal.abstractqueryimpl.determinetype(abstractqueryimpl.java:507) @ org.hibernate.internal.abstractqueryimpl.setparameter(abstractqueryimpl.java:479) @ com.comresource.scrapmetalapp.daoimpl.customerdaoimpl.searchcustomer(customerdaoimpl.java:61) @ com.comresource.scrapmetalapp.serviceimpl.customerserviceimpl.searchcustomer(customerserviceimpl.java:39) @ com.comresource.scrapmetalapp.config.run.main(run.java:57)
my dao implementation this:
@override public customer searchcustomer(string fname, string lname, integer company) { session session = sessionfactory.opensession(); return (customer) session.getnamedquery("getcustomer").setparameter(1, fname) .setparameter(2, lname) .setparameter(3, company) .uniqueresult(); }
what issue here?
for this, need see how associating mapping in model class, query should go this.
public customer getmethatcustomer(string param1, string param2, int foreignkey){ session = getcurrentsession(); org.hibernate.query query = session.createquery("from customer c c.name=:param1 , c.lastname=:param2 , c.company.companyid=:foreignkey"); //note last parameter, have mentioned c.company, in place of company, there should foregin key association , primary key in java class. query.setparameter("param1",param1); query.setp...er("param2",param2); quer.....("companyid",companyid); return (customer) query.uniqueresult(); }
so, try out, let me know if there problem
Comments
Post a Comment