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" ......
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.
<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.
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.