• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

flush is not deleting records in hibernate

 
shikhaj jainy
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am using createSQLQuery in hibernate to delete records on condition basis:



On console,result is printing right number of records but in db it is not deleting any record even with flush() method. Is there anything i need to add?


Thanks .
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I might be wrong here, but flush() isn't a commit().
It just pushes stuff to the db, but the transaction isn't committed, so the changes aren't visible outside that session.
 
shikhaj jainy
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,

Got this "flush() will synchronize your database with the current state of object/objects held in the memory" from

http://stackoverflow.com/questions/14581865/hibernate-commit-and-flush

By synchronize means ..db shud be updated with deleted records after executing flush? But in db still records were there just after flush()

Thanks a lot.
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shikhaj jainy wrote:Hi Dave,

Got this "flush() will synchronize your database with the current state of object/objects held in the memory" from

http://stackoverflow.com/questions/14581865/hibernate-commit-and-flush

By synchronize means ..db shud be updated with deleted records after executing flush? But in db still records were there just after flush()

Thanks a lot.


Which isn't a commit.

Do you commit your transaction?

Can you show how you open and complete your transaction, that this DELETE is part of?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you commit your transaction?
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, didn't notice Dave had already asked exactly the same question
 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roel De Nijs wrote:Oops, didn't notice Dave had already asked exactly the same question


Echo...echo...cho...oh

 
shikhaj jainy
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I used em.getTransaction().commit() to do commit explicitly...it did not work...then i added @Transactional in lower layer where dao class is called..no result..I am using weblogic....in my code nowhere i see @transactional or commit() method explicitly..so does weblogic take cares of transaction..but then why not for my delete method?

I tried below:



but in db , still rows are there.
Please let me know how to proceed with this.

Thanks.

 
Dave Tolls
Ranch Hand
Posts: 2112
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Changing to a SELECT then a load of indiviidual removes is not the answer.

Without actually seeing your transactional boundaries then this is going to be almost impossible to diagnose.
If (as your earlier code showed) the DELETE statement is resulting in rows being deleted (the count returned) then there are one of two things happening...either you are not looking at the same db as your app (this does happen, believe me...I'm very prone to making this mistake), or the transaction is not committing.

In the latter case we would need to see actual code that covers your transaction. Where are the boundaries, what are your transactions set to as far as commit and rollback, are you getting any exceptions anywhere?
It is not going to be solved by looking at your query itself, as that apparently runs OK.
 
shikhaj jainy
Ranch Hand
Posts: 35
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave ,

First is not the case

I found below in one of db xmls:



So does this mean transaction is also managed by weblogic(not aware much in this area?)

Thanks a lot.
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how in your code are transactions defined? Do you use @Transactional on methods? Or do you manage them programmatically?

You just shared the persistence.xml but that's not the actual code that covers your transaction (like Dave already asked).
 
Roel De Nijs
Sheriff
Posts: 10666
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
shikhaj jainy wrote:then i added @Transactional in lower layer where dao class is called..no result..

That's quite normal that you don't see any result if you @Transactional in a lower layer where dao class is used. Because your code in your original post doesn't use this layer at all (or you are not showing all code being used), so if you change code that's unused, it won't have any effect.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic