Hibernate Problematic Flush Mode

ID

java.hibernate_problematic_flush_mode

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

hibernate, orm

Description

Reports calls to session.setFlushMode(FlushMode.MANUAL), FlushMode.NEVER, or FlushMode.COMMIT. These modes disable or delay automatic flushing, meaning changes to persistent entities may be silently lost.

Rationale

MANUAL and NEVER disable automatic flushing entirely, requiring explicit session.flush() calls. COMMIT only flushes at transaction commit, which can cause stale reads within the same transaction. All three modes are common sources of data-loss bugs.

// Bad -- changes may be lost
session.setFlushMode(FlushMode.MANUAL);

Remediation

Use FlushMode.AUTO (the default) unless you have a specific reason to control flushing manually, and ensure proper flush() calls in that case.

session.setFlushMode(FlushMode.AUTO);