Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

{Hibernate} Is it possible

 
Dyann Sri
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • 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" ......
 
Joe Matthew
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Gregg Bolinger
GenRocket Founder
Ranch Hand
Posts: 15302
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
 
Eric Nielsen
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • 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.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is also Interceptors that can be used to interject some code in the CRUD calls.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic