• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Hibernate: could not set a field value by reflection setter

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

http://krams915.blogspot.com/2011/01/spring-mvc-3-hibernate-annotations.html

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:

https://coderanch.com/t/590071/ORM/databases/org-hibernate-SessionException-Session-closed#2687293
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh and welcome to Java Ranch
 
Jozo Stan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your advices .

This are parts of my code files:

SportRepositoryDAO.java




SportRepositoryDAOHibernate.java




SportService.java




SportServiceImpl.java





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.
 
Bill Gorder
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic