Interface NativeQuery<T>

All Superinterfaces:
CommonQueryContract, MutationQuery, Query, Query<T>, SelectionQuery<T>, SynchronizeableQuery, TypedQuery<T>
All Known Subinterfaces:
NativeQueryImplementor<R>
All Known Implementing Classes:
NativeQueryImpl

public interface NativeQuery<T> extends Query<T>, SynchronizeableQuery
Within the context of an active session, an instance of this type represents an executable query written in the native SQL dialect of the underlying database. Since Hibernate does not actually understand SQL, it often requires some help in interpreting the semantics of a native SQL query.

Along with the operations inherited from Query, this interface provides control over:

  • mapping the result set of the native SQL query, and
  • synchronization of the database with state held in memory before execution of the query, via automatic flushing of the session.

A NativeQuery may be obtained from the Session by calling:

A result set mapping may be specified by:

The third option is a legacy of much older versions of Hibernate and is currently disfavored.

To determine if an automatic flush is required before execution of the query, Hibernate must know which tables affect the query result set. JPA provides no standard way to do this. Instead, this information may be provided via:

When the affected tables are not known to Hibernate, the behavior depends on whether Hibernate is operating in fully JPA-compliant mode.

  • In JPA-compliant mode, FlushModeType.AUTO specifies that the session should be flushed before execution of a native query when the affected tables are not known.
  • Otherwise, when Hibernate is not operating in JPA-compliant mode, AUTO specifies that the session is not flushed before execution of a native query, unless the affected tables are known and Hibernate determines that a flush is required.
See Also: