• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

How to test Hibernate dao layer in spring?

 
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I have test method within spring test class:

Method save is not flushing new instance into database. Therefore if I go to database I have nothing persisted.
I tried to begin transaction before save method and commit it after. I tried to open new session and create new transaction ...
Anyway I receive error: org.hibernate.HibernateException: No Session found for current thread
This spring test method is not running within transaction.

How to tell hibernate to flush all data to database after calling method save?

Thank you!
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there is a flush method in session.

you can add @Rollback annotation to your test method

 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reply!

session.flush() method flushes data into db when transaction is committed.
If I make separate method that calls hibernate save and annotate it with @Transactional :

I receive the same error org.hibernate.HibernateException: No Session found for current thread.
Exactly the same error I have if I try to begin transaction and commit it in doSave already without @Transactional

Exactly the same error I have if I try to open new session begin new tranaction in doSave method.

How to flush data into database after calling hibernate method save ?
 
Bin Smith
Ranch Hand
Posts: 514
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
FInally I achived what I wanted!

I suspended spring declarative transaction for my @Test method and used spring programmatic transaction support (TransactionTemplate) right around my hibernate save method:

Now after doSave method I access database with spring jdbcTemplate and it returns exact the same instance I saved with hibernate!
IF I use declarative transaction @Transactional for @Test method this doSave method does not flush data to database, instead only at the end of @Test method my answer is persisted. Of course I can't persist answer in one @Test method and retrieve it in next @Test method!

Thanks for help!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic