• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

{Hibernate} Is it possible

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We are using Hibernate as ORM framework . Is it possible to save the records without using Hibernate API's like save , saveOrUPdate explicitly . Because somewhere I observed HQL query like "Update ***" had been generated , but we didnt use any Hibernate API's to do that for us . Is it happened because of "new" ......
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I believe that more information would be great on the query being generated automatically. Post the query as well.

Hibernate should not run any automatic queries unless you are having the auto commit = true. This will generate commits automatically so you might see some action done on the DB side.
 
Dyann Sri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oK , Well , i will put other way , what could be the other scenarios ( in addition to save or saveOrUpdtae) which cause transient state to persistent state.
 
Ranch Hand
Posts: 15304
6
Mac OS X IntelliJ IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The real question is not what queries you might see in the debug but what is happening in the database. Hibernate will generate and cache some pseudo queries during some operations. Especially when using annotations, I've noticed. That doesn't mean it is actually executing the queries. It's just preparing itself for when it needs to. So yea, more information from you is needed probably.
 
author and cow tipper
Posts: 5009
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<sarcasm>Oh, don't think you can control when Hibernate will do an update!!!</sarcasm>

You'll be surprised by when Hibernate will update the database. It certainly does not depend on a save or saveOrUpdate call. For example, if Hibernate has a changed entity in its cache, and then it needs to do a query, you will see Hibernate update the database and then do the SQL query, even though no update was called - Hibernate simply saw a state change in the entity.

Yes, I can definitely see this type of scenario happen.

-Cameron McKenzie
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes to elaborate:

a) a call to flush() on the session/entityManager will cause all dirty objects to be written to the database

b) just about any query causes an implicit flush of dirty data, in order to ensure that the query returns correct results

There are some workflows where this common causes problems and work-arounds can prove difficult. But basically you want to avoid dirtying an object until you are ready to either commit it or rollback immediately... and avoid leaving dirty objects attached to the context (session or entityManager). If you're using native hibernate interfaces you can explitly detach a dirty object with the evict(object) call. Of course you'll have to re-attach it later if you want to save the changes.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is also Interceptors that can be used to interject some code in the CRUD calls.

Mark
 
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic