I'm trying to pass a method to controller, in a project using Java 8, Spring and Hibernate. But I'm having problems with "Request processing failed; nested exception is java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer" error.
My method from KomitentDAO looks like this
@Override
public int vratiBroj() {
Session currentSession = sessionFactory.getCurrentSession();
int num = 0;
Query query = currentSession.createQuery("SELECT count(*) from
Komitent");
num = (int) query.getSingleResult();
return num;
}
And Controller loks like this
@GetMapping("/dodaj")
public String prikaziForm(Model theModel) {
Komitent komt = new Komitent();
int a = komitentService.vratiBroj();
komt.setSifra(a);
theModel.addAttribute("komitent", komt);
return "komitent-form";
}
I need to set in komt.Sifra an Integer from my function. The query is just a test, it could be any other that return a single int. What am I doing wrong and is there a better way for querying databases and returning values to Model?
count(*) does returns Long not Integer.
したがって、メソッドは次のようになります。
public int vratiBroj() {
Session currentSession = sessionFactory.getCurrentSession();
Query query = currentSession.createQuery("SELECT count(*) from Komitent");
long num = (Long) query.getSingleResult();
return num.intValue();
}
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加