Hibernate: could not set a field value by reflection setter
Jozo Stan
Greenhorn
Joined: Aug 18, 2012
Posts: 11
posted
0
Hello everybody!
I have a problem with hibernate exception when i want to insert data to db. This is a part of STS trace.
Java code:
Sport.java
Drzava means Country.
And the part of jsp
When I start the web application and open the page for inserting new Sport for a country, I choose a country and then i write for example basketball. Then I press submit and the exception is there. In db the sport ID is not NULL and I don't know where is the mistake. I've also found this http://blogs.warwick.ac.uk/colinyates/entry/i_hate_hibernate/ but I don't have such a mistake.
Please help.
Thanks in advance!
Jozo
Edited line length on the stack trace to prevent horizontal scroll bars. - Bill
Well things were looking pretty good until I got to your JSP
Rule #1 - Don't put any persistence/DB stuff in your JSP/view layer ever Rule #2 - Don't use scriptlets in your JSP
Your Spring controller looks pretty normal so we won't focus to much on that yet. You apparently have a service what is in there? You should also have a Repository (persistence layer) which is called by your service. This is where ALL of the data access stuff belongs (not in your jsp). Hibernate can be used to retrieve all your objects too. this list of objects can than be added to your model and displayed in your option select.
All of your db configuration user name password etc belongs in your Spring config.
I think I would try a tutorial project. As far as I can see the blog you listed is not really relevant to your problem.
Try this tutorial: Note this one is a little over simplified in that the hibernate stuff really should be in the persistence layer but it gives you an idea for the controller to JSP part.
For the separation between the service and repository layer (which appears to be non-existent in the blog I linked) take a look at my suggestions in this thread:
About the persistence/DB stuff in my JSP. Unfortunately, I didn't know if there exists another way to populate my drop down box. I found this on the web.
It looks like you are on the right track. I would use the @Repository on your DAO for the reason I mentioned in the second link I provided. Also In you DAO don't use hibernateTemplate unless you have opted to use a very old version of hibernate (not recommended). With newer versions this is no longer considered to be the best practice instead follow the examples in the links I provided.
I would also use the newest version of Spring as many things are improved as time goes on. Currently that is 3.1.2-RELEASE
About the persistence/DB stuff in my JSP. Unfortunately, I didn't know if there exists another way to populate my drop down box. I found this on the web.
That web page should be purged from the Internet!
Once again read through the tutorial I linked and the post I made in that other thread. Implement what you learn from it and post back here or in our Spring forums with any specific problems you might encounter.