简体   繁体   中英

Hibernate No value specified for parameter

I have a named query that retrieves UgAcyearExamSchedule interms of a LIST as follows:

@NamedQuery(name="UgAcyearExamSchedule.getByDetail", 
query="select u FROM UgAcyearExamSchedule u where 
u.ugAcyearExamScheduleDetails=:ugDetails")

where ugAcyearExamScheduleDetails is defined in UgAcyearExamSchedule as follows:

@OneToMany(mappedBy="ugAcyearExamSchedule")
private List<UgAcyearExamScheduleDetail> ugAcyearExamScheduleDetails;

when i tried to set the parameter for these query as follows:

Query query = em.createNamedQuery("UgAcyearExamSchedule.getByDetailAndDeptYear");
query.setParameter("ugDetails",details);
List<UgAcyearExamSchedule> examSChedules = (List<UgAcyearExamSchedule>) query.getResultList();

the following exception arrises:

ERROR: No value specified for parameter
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet

I don't know the main cause of these problem,Thanks in advance

You define a named query with name " UgAcyearExamSchedule.getByDetail " yet you are invoking the named query with name " UgAcyearExamSchedule.getByDetailAndDeptYear ". I'd suggest that you have an extra parameter (deptYear?) on this other named query (that you haven't posted), so it complains about the extra parameter.

Also you cannot really use "collectionField = :collectionParam" in JPQL. It is more portable to check on the "MEMBER OF" for the elements.

I believe you are not using Named Queries correctly. I noticed you use the createNamedQuery method, however your Named Query already exists; you need to call it.

I suppose that your em stands for EntityManager or similar, so if you are making your queries with an instance of Hibernate Session Factory you should try using getNamedQuery instead:

Query query = session.getNamedQuery("UgAcyearExamSchedule.getByDetailAndDeptYear")
             .setParameter("ugDetails",details);

Make Sure that your details variable is actually set to something. Also make sure that you are using Hibernate Session correctly.

Some links that may help you :)

http://www.mkyong.com/hibernate/hibernate-parameter-binding-examples/

http://www.mkyong.com/hibernate/hibernate-named-query-examples/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM