aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate: could not set a field value by reflection setter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: could not set a field value by reflection setter" Watch "Hibernate: could not set a field value by reflection setter" New topic
Author

Hibernate: could not set a field value by reflection setter

Jozo Stan
Greenhorn

Joined: Aug 18, 2012
Posts: 12
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
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

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:

http://www.coderanch.com/t/590071/ORM/databases/org-hibernate-SessionException-Session-closed#2687293


[How To Ask Questions][Read before you PM me]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1676
    
    7

Oh and welcome to Java Ranch
Jozo Stan
Greenhorn

Joined: Aug 18, 2012
Posts: 12
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

Joined: Mar 07, 2010
Posts: 1676
    
    7

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Hibernate: could not set a field value by reflection setter