aspose file tools*
The moose likes Object Relational Mapping and the fly likes {Hibernate} Is it possible 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} Is it possible" Watch "{Hibernate} Is it possible" New topic
Author

{Hibernate} Is it possible

Dyann Sri
Greenhorn

Joined: Dec 13, 2007
Posts: 27
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

Joined: Jun 10, 2008
Posts: 66
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.


Regards,<br />Joe<br /> <br />"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live."<br />--Martin Golding
Dyann Sri
Greenhorn

Joined: Dec 13, 2007
Posts: 27
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

Joined: Jul 11, 2001
Posts: 15299
    
    6

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.


GenRocket - Experts at Building Test Data
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

<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

Joined: Dec 14, 2004
Posts: 194
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

Joined: Feb 05, 2001
Posts: 17257
    
    6

There is also Interceptors that can be used to interject some code in the CRUD calls.

Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: {Hibernate} Is it possible