Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

java - What to use Flush Mode 'Auto' or 'Commit'

As my title described, I am using hibernate Auto flush mode mechanism in my application. So, when I change any data in a hibernate persistent object, it reflects automatically in the database. I don't want this. So I found a solution to use FlushMode Commit instead.

So here is my actual question:

  • Is it better to use Commit flush mode instead of Auto? and
  • What is the meaning of this statement from the documentation?

    The Session is sometimes flushed before query execution in order to ensure that queries never return stale state.

http://docs.jboss.org/hibernate/orm/3.5/javadoc/org/hibernate/FlushMode.html

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Hibernate (and JPA) are designed to automatically detect and persist changes to persistent objects to the database. There is no "save" operation.

If you don't want things saved, you should use detached objects. Either use a StatelessSession to load them, or call detach after loading your objects. This removes them from the monitoring that will automatically save them.

Don't mess with the flush settings, it will just give you headaches later.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...