File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes hibernate not saving data with no error 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 not saving data with no error" Watch "hibernate not saving data with no error" New topic
Author

hibernate not saving data with no error

Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
hello
i have an app with spring and hibernate. When i run the app on my local it works perfectly and saves and updates user. where when i run in my test container there is no error but at the same time it does not save in database.
The only difference when i run on my local is i use oracle.jdbc.pool.OracleDataSource were as on my test box i used tomcat.dbcp.connection pool.

i came to know thru some threads to use @Transactional attribute since spring needs to know that the update or delete is a part of transaction.After adding annotation at the begining of the method i still does not save to database.

Does anyone has any idea or advice

Thanks
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Why are you trying to commit in your testing phase.

This is why in-memory databases are commonly uses for test cases.


WP
Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
sorry when i say test i mean when i run the app in my test environment which runs on Tomcat

The other thing is that this incident came up recently our app was working fine, i thought when we wire hibernate thru spring commit should happen automatically and should not be called explicity.
i also tried annotating a method using @Transactional but still the same behaviour

Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

Perhaps on your local data source you have autocommit set to true which is allowing things to commit. This typically would not be what you want as you want to group operations into a transaction not autocommit them. You are probably configuring your declaritive transactions incorrectly in Spring. Are you applying them only to public methods exposed by an interface and accessing them throough the interface? Spring by default uses java interface based proxies for transactions. If you do not adhere to those rules you need to use AspectJ or proxy target classes rather than interfaces. I would read through the Spring documentation on this you can also see if my post here helps you.

Short story long (by default) if there is no interface there is no transaction.

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


[How To Ask Questions][Read before you PM me]
Darvesh Niz
Ranch Hand

Joined: May 12, 2008
Posts: 119
You might be correct.
Right now i used .flush() method to flush out my changes to the database.

Also have noticed @Transactional annotation on my implementing class and not the interface.

You mean to say in order for spring to treat it as a transaction you have to put annotation on the interface rather than implementing classes.

Rashid
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

No, that is not what I meant. Have a look at the link I posted above, I gave some examples there. The annotations go on the implementation class, what I mean is that there must be an interface and you should be accessing that class through its interface.

In other words you should see this





Not this



That is what I meant by access it through its interface.

Almost every time someone has the problem you are experiencing it is due to transaction issues. If you want to test to see if you really have a transaction try this inside your @Transactional method

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: hibernate not saving data with no error