Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate;
import java.util.List;
import java.util.function.Consumer;
import jakarta.persistence.FindOption;
import org.hibernate.graph.RootGraph;
import org.hibernate.jdbc.Work;
import org.hibernate.query.Query;
import org.hibernate.stat.SessionStatistics;
import jakarta.persistence.CacheRetrieveMode;
import jakarta.persistence.CacheStoreMode;
import jakarta.persistence.EntityGraph;
import jakarta.persistence.EntityManager;
import jakarta.persistence.FlushModeType;
import jakarta.persistence.LockModeType;
import jakarta.persistence.TypedQueryReference;
import jakarta.persistence.criteria.CriteriaDelete;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.CriteriaUpdate;
/**
* The main runtime interface between a Java application and Hibernate. Represents the
* notion of a persistence context, a set of managed entity instances associated
* with a logical transaction.
*
* The lifecycle of a {@code Session} is bounded by the beginning and end of the logical
* transaction. But a long logical transaction might span several database transactions.
*
* The primary purpose of the {@code Session} is to offer create, read, and delete
* operations for instances of mapped entity classes. An instance may be in one of three
* states with respect to a given open session:
*
*
transient: never persistent, and not associated with the {@code Session},
*
persistent: currently associated with the {@code Session}, or
*
detached: previously persistent, but not currently associated with the
* {@code Session}.
*
*
* At any given time, an instance may be associated with at most one open session.
*
* Any instance returned by {@link #get(Class, Object)}, {@link #find(Class, Object)},
* or by a query is persistent. A persistent instance might hold references to other
* entity instances, and sometimes these references are proxied by an
* intermediate object. When an associated entity has not yet been fetched from the
* database, references to the unfetched entity are represented by uninitialized
* proxies. The state of an unfetched entity is automatically fetched from the
* database when a method of its proxy is invoked, if and only if the proxy is
* associated with an open session. Otherwise, {@link #getReference(Object)} may be
* used to trade a proxy belonging to a closed session for a new proxy associated
* with the current session.
*
* A transient instance may be made persistent by calling {@link #persist(Object)}.
* A persistent instance may be made detached by calling {@link #detach(Object)}.
* A persistent instance may be marked for removal, and eventually made transient, by
* calling {@link #remove(Object)}.
*
* Persistent instances are held in a managed state by the persistence context. Any
* change to the state of a persistent instance is automatically detected and eventually
* flushed to the database. This process of automatic change detection is called
* dirty checking and can be expensive in some circumstances. Dirty checking
* may be disabled by marking an entity as read-only using
* {@link #setReadOnly(Object, boolean)} or simply by {@linkplain #detach(Object) evicting}
* it from the persistence context. A session may be set to load entities as read-only
* {@linkplain #setDefaultReadOnly(boolean) by default}, or this may be controlled at the
* {@linkplain Query#setReadOnly(boolean) query level}.
*
* The state of a transient or detached instance may be made persistent by copying it to
* a persistent instance using {@link #merge(Object)}. All older operations which moved a
* detached instance to the persistent state are now deprecated, and clients should now
* migrate to the use of {@code merge()}.
*
* The persistent state of a managed entity may be refreshed from the database, discarding
* all modifications to the object held in memory, by calling {@link #refresh(Object)}.
*
* From {@linkplain FlushMode time to time}, a {@linkplain #flush() flush operation} is
* triggered, and the session synchronizes state held in memory with persistent state
* held in the database by executing SQL {@code insert}, {@code update}, and {@code delete}
* statements. Note that SQL statements are often not executed synchronously by the methods
* of the {@code Session} interface. If synchronous execution of SQL is desired, the
* {@link StatelessSession} allows this.
*
* Each managed instance has an associated {@link LockMode}. By default, the session
* obtains only {@link LockMode#READ} on an entity instance it reads from the database
* and {@link LockMode#WRITE} on an entity instance it writes to the database. This
* behavior is appropriate for programs which use optimistic locking.
*
*
A different lock level may be obtained by explicitly specifying the mode using
* {@link #get(Class, Object, LockMode)}, {@link #find(Class, Object, LockModeType)},
* {@link #refresh(Object, LockMode)}, {@link #refresh(Object, LockModeType)}, or
* {@link org.hibernate.query.SelectionQuery#setLockMode(LockModeType)}.
*
The lock level of a managed instance already held by the session may be upgraded
* to a more restrictive lock level by calling {@link #lock(Object, LockMode)} or
* {@link #lock(Object, LockModeType)}.
*
*
* A persistence context holds hard references to all its entities and prevents them
* from being garbage collected. Therefore, a {@code Session} is a short-lived object,
* and must be discarded as soon as a logical transaction ends. In extreme cases,
* {@link #clear()} and {@link #detach(Object)} may be used to control memory usage.
* However, for processes which read many entities, a {@link StatelessSession} should
* be used.
*
* A session might be associated with a container-managed JTA transaction, or it might be
* in control of its own resource-local database transaction. In the case of a
* resource-local transaction, the client must demarcate the beginning and end of the
* transaction using a {@link Transaction}. A typical resource-local transaction should
* use the following idiom:
*
* It's crucially important to appreciate the following restrictions and why they exist:
*
*
If the {@code Session} throws an exception, the current transaction must be rolled
* back and the session must be discarded. The internal state of the {@code Session}
* cannot be expected to be consistent with the database after the exception occurs.
*
At the end of a logical transaction, the session must be explicitly {@linkplain
* #close() destroyed}, so that all JDBC resources may be released.
*
A {@code Session} is never thread-safe. It contains various different sorts of
* fragile mutable state. Each thread or transaction must obtain its own dedicated
* instance from the {@link SessionFactory}.
*
*
* An easy way to be sure that session and transaction management is being done correctly
* is to {@linkplain SessionFactory#inTransaction(Consumer) let the factory do it}:
*
* sessionFactory.inTransaction(session -> {
* //do the work
* ...
* });
*
*
* A session may be used to {@linkplain #doWork(Work) execute JDBC work} using its JDBC
* connection and transaction:
*
* A {@code Session} instance is serializable if its entities are serializable.
*
* Every {@code Session} is a JPA {@link EntityManager}. Furthermore, when Hibernate is
* acting as the JPA persistence provider, the method {@link EntityManager#unwrap(Class)}
* may be used to obtain the underlying {@code Session}.
*
* Hibernate, unlike JPA, allows a persistence unit where an entity class is mapped multiple
* times, with different entity names, usually to different tables. In this case, the session
* needs a way to identify the entity name of a given instance of the entity class. Therefore,
* some operations of this interface, including operations inherited from {@code EntityManager},
* are overloaded with a form that accepts an explicit entity name along with the instance. An
* alternative solution to this problem is to provide an {@link EntityNameResolver}.
*
* @see SessionFactory
*
* @author Gavin King
* @author Steve Ebersole
*/
public interface Session extends SharedSessionContract, EntityManager {
/**
* Force this session to flush. Must be called at the end of a unit of work,
* before the transaction is committed. Depending on the current
* {@linkplain #setHibernateFlushMode(FlushMode) flush mode}, the session might
* automatically flush when {@link Transaction#commit()} is called, and it is not
* necessary to call this method directly.
*
* Flushing is the process of synchronizing the underlying persistent
* store with persistable state held in memory.
*
* @throws HibernateException if changes could not be synchronized with the database
*/
@Override
void flush();
/**
* Set the current {@linkplain FlushModeType JPA flush mode} for this session.
*
* Flushing is the process of synchronizing the underlying persistent
* store with persistable state held in memory. The current flush mode determines
* when the session is automatically flushed.
*
* @param flushMode the new {@link FlushModeType}
*
* @see #setHibernateFlushMode(FlushMode)
*/
@Override
void setFlushMode(FlushModeType flushMode);
/**
* Set the current {@linkplain FlushMode flush mode} for this session.
*
* Flushing is the process of synchronizing the underlying persistent
* store with persistable state held in memory. The current flush mode determines
* when the session is automatically flushed.
*
* The {@linkplain FlushMode#AUTO default flush mode} is sometimes unnecessarily
* aggressive. For a logically "read only" session, it's reasonable to set the
* session's flush mode to {@link FlushMode#MANUAL} at the start of the session
* in order to avoid some unnecessary work.
*
* Note that {@link FlushMode} defines more options than {@link FlushModeType}.
*
* @param flushMode the new {@link FlushMode}
*/
void setHibernateFlushMode(FlushMode flushMode);
/**
* Get the current {@linkplain FlushModeType JPA flush mode} for this session.
*
* @return the {@link FlushModeType} currently in effect
*
* @see #getHibernateFlushMode()
*/
@Override
FlushModeType getFlushMode();
/**
* Get the current {@linkplain FlushMode flush mode} for this session.
*
* @return the {@link FlushMode} currently in effect
*/
FlushMode getHibernateFlushMode();
/**
* Set the current {@linkplain CacheMode cache mode} for this session.
*
* The cache mode determines the manner in which this session can interact with
* the second level cache.
*
* @param cacheMode the new cache mode
*/
void setCacheMode(CacheMode cacheMode);
/**
* Get the current {@linkplain CacheMode cache mode} for this session.
*
* @return the current cache mode
*/
CacheMode getCacheMode();
/**
* The JPA-defined {@link CacheStoreMode}.
*
* @see #getCacheMode()
*
* @since 6.2
*/
@Override
CacheStoreMode getCacheStoreMode();
/**
* The JPA-defined {@link CacheRetrieveMode}.
*
* @see #getCacheMode()
*
* @since 6.2
*/
@Override
CacheRetrieveMode getCacheRetrieveMode();
/**
* Enable or disable writes to the second-level cache.
*
* @param cacheStoreMode a JPA-defined {@link CacheStoreMode}
*
* @see #setCacheMode(CacheMode)
*
* @since 6.2
*/
@Override
void setCacheStoreMode(CacheStoreMode cacheStoreMode);
/**
* Enable or disable reads from the second-level cache.
*
* @param cacheRetrieveMode a JPA-defined {@link CacheRetrieveMode}
*
* @see #setCacheMode(CacheMode)
*
* @since 6.2
*/
@Override
void setCacheRetrieveMode(CacheRetrieveMode cacheRetrieveMode);
/**
* Get the maximum batch size for batch fetching associations by
* id in this session.
*
* @since 6.3
*/
int getFetchBatchSize();
/**
* Set the maximum batch size for batch fetching associations by
* id in this session. Override the
* {@linkplain org.hibernate.boot.spi.SessionFactoryOptions#getDefaultBatchFetchSize()
* factory-level} default controlled by the configuration property
* {@value org.hibernate.cfg.AvailableSettings#DEFAULT_BATCH_FETCH_SIZE}.
*
*
*
If {@code batchSize>1}, then batch fetching is enabled.
*
If {@code batchSize<0}, the batch size is inherited from
* the factory-level setting.
*
Otherwise, batch fetching is disabled.
*
*
* @param batchSize the maximum batch size for batch fetching
*
* @since 6.3
*
* @see org.hibernate.cfg.AvailableSettings#DEFAULT_BATCH_FETCH_SIZE
*/
void setFetchBatchSize(int batchSize);
/**
* Determine if subselect fetching is enabled in this session.
*
* @return {@code true} is subselect fetching is enabled
*
* @since 6.3
*/
boolean isSubselectFetchingEnabled();
/**
* Enable or disable subselect fetching in this session. Override the
* {@linkplain org.hibernate.boot.spi.SessionFactoryOptions#isSubselectFetchEnabled()
* factory-level} default controlled by the configuration property
* {@value org.hibernate.cfg.AvailableSettings#USE_SUBSELECT_FETCH}.
*
* @param enabled {@code true} to enable subselect fetching
*
* @since 6.3
*
* @see org.hibernate.cfg.AvailableSettings#USE_SUBSELECT_FETCH
*/
void setSubselectFetchingEnabled(boolean enabled);
/**
* Get the session factory which created this session.
*
* @return the session factory
*
* @see SessionFactory
*/
SessionFactory getSessionFactory();
/**
* Cancel the execution of the current query.
*
* This is the sole method on session which may be safely called from
* another thread.
*
* @throws HibernateException if there was a problem cancelling the query
*/
void cancelQuery();
/**
* Does this session contain any changes which must be synchronized with
* the database? In other words, would any DML operations be executed if
* we flushed this session?
*
* @return {@code true} if the session contains pending changes; {@code false} otherwise.
* @throws HibernateException could not perform dirtying checking
*/
boolean isDirty();
/**
* Will entities and proxies that are loaded into this session be made
* read-only by default?
*
* To determine the read-only/modifiable setting for a particular entity
* or proxy use {@link #isReadOnly(Object)}.
*
* @see #isReadOnly(Object)
*
* @return {@code true}, loaded entities/proxies will be made read-only by default;
* {@code false}, loaded entities/proxies will be made modifiable by default.
*/
boolean isDefaultReadOnly();
/**
* Change the default for entities and proxies loaded into this session
* from modifiable to read-only mode, or from modifiable to read-only mode.
*
* Read-only entities are not dirty-checked and snapshots of persistent
* state are not maintained. Read-only entities can be modified, but
* changes are not persisted.
*
* When a proxy is initialized, the loaded entity will have the same
* read-only/modifiable setting as the uninitialized proxy has,
* regardless of the session's current setting.
*
* To change the read-only/modifiable setting for a particular entity
* or proxy that already belongs to this session use
* {@link #setReadOnly(Object, boolean)}.
*
* To override this session's read-only/modifiable setting for all
* entities and proxies loaded by a certain {@code Query} use
* {@link Query#setReadOnly(boolean)}.
*
* @see #setReadOnly(Object,boolean)
* @see Query#setReadOnly(boolean)
*
* @param readOnly {@code true}, the default for loaded entities/proxies is read-only;
* {@code false}, the default for loaded entities/proxies is modifiable
*/
void setDefaultReadOnly(boolean readOnly);
/**
* Return the identifier value of the given entity associated with this session.
* An exception is thrown if the given entity instance is transient or detached
* in relation to this session.
*
* @param object a persistent instance associated with this session
*
* @return the identifier
*
* @throws TransientObjectException if the instance is transient or associated with
* a different session
*/
Object getIdentifier(Object object);
/**
* Determine if the given entity is associated with this session.
*
* @param entityName the entity name
* @param object an instance of a persistent class
*
* @return {@code true} if the given instance is associated with this {@code Session}
*/
boolean contains(String entityName, Object object);
/**
* Remove this instance from the session cache. Changes to the instance will
* not be synchronized with the database. This operation cascades to associated
* instances if the association is mapped with
* {@link jakarta.persistence.CascadeType#DETACH}.
*
* @param object the managed instance to detach
*/
@Override
void detach(Object object);
/**
* Remove this instance from the session cache. Changes to the instance will
* not be synchronized with the database. This operation cascades to associated
* instances if the association is mapped with
* {@link jakarta.persistence.CascadeType#DETACH}.
*
* This operation is a synonym for {@link #detach(Object)}.
*
* @param object the managed entity to evict
*
* @throws NullPointerException if the passed object is {@code null}
* @throws IllegalArgumentException if the passed object is not mapped as an entity
*/
void evict(Object object);
/**
* Return the persistent instance of the given entity class with the given identifier,
* or null if there is no such persistent instance. If the instance is already associated
* with the session, return that instance. This method never returns an uninitialized
* instance.
*
* The object returned by {@code get()} or {@code find()} is either an unproxied instance
* of the given entity class, or a fully-fetched proxy object.
*
* This operation requests {@link LockMode#NONE}, that is, no lock, allowing the object
* to be retrieved from the cache without the cost of database access. However, if it is
* necessary to read the state from the database, the object will be returned with the
* lock mode {@link LockMode#READ}.
*
* To bypass the {@linkplain Cache second-level cache}, and ensure that the state of the
* requested instance is read directly from the database, either:
*
*
call {@link #find(Class, Object, FindOption...)}, passing
* {@link CacheRetrieveMode#BYPASS} as an option,
*
call {@link #find(Class, Object, LockMode)} with the explicit lock mode
* {@link LockMode#READ}, or
*
{@linkplain #setCacheRetrieveMode set the cache mode} to
* {@link CacheRetrieveMode#BYPASS} before calling this method.
*
*
* @apiNote This operation is very similar to {@link #get(Class, Object)}.
*
* @param entityType the entity type
* @param id an identifier
*
* @return a fully-fetched persistent instance or null
*/
@Override
T find(Class entityType, Object id);
/**
* Return the persistent instance of the given entity class with the given identifier,
* or null if there is no such persistent instance. If the instance is already associated
* with the session, return that instance. This method never returns an uninitialized
* instance. Obtain the specified lock mode if the instance exists.
*
* Convenient form of {@link #find(Class, Object, LockOptions)}.
*
* @param entityType the entity type
* @param id an identifier
* @param lockMode the lock mode
*
* @return a fully-fetched persistent instance or null
*
* @since 7.0
*
* @see #find(Class, Object, LockOptions)
*/
T find(Class entityType, Object id, LockMode lockMode);
/**
* Return the persistent instance of the given entity class with the given identifier,
* or null if there is no such persistent instance. If the instance is already associated
* with the session, return that instance. This method never returns an uninitialized
* instance. Obtain the specified lock mode if the instance exists.
*
* @param entityType the entity type
* @param id an identifier
* @param lockOptions the lock mode
*
* @return a fully-fetched persistent instance or null
*
* @since 7.0
*/
T find(Class entityType, Object id, LockOptions lockOptions);
/**
* Return the persistent instances of the given entity class with the given identifiers
* as a list. The position of an instance in the returned list matches the position of its
* identifier in the given list of identifiers, and the returned list contains a null value
* if there is no persistent instance matching a given identifier. If an instance is already
* associated with the session, that instance is returned. This method never returns an
* uninitialized instance.
*
* Every object returned by {@code findMultiple()} is either an unproxied instance of the
* given entity class, or a fully-fetched proxy object.
*
* This method accepts {@link BatchSize} as an option, allowing control over the number of
* records retrieved in a single database request. The performance impact of setting a batch
* size depends on whether a SQL array may be used to pass the list of identifiers to the
* database:
*
*
for databases which {@linkplain org.hibernate.dialect.Dialect#supportsStandardArrays
* support standard SQL arrays}, a smaller batch size might be extremely inefficient
* compared to a very large batch size or no batching at all, but
*
on the other hand, for databases with no SQL array type, a large batch size results
* in long SQL statements with many JDBC parameters.
*
*
* For more advanced cases, use {@link #byMultipleIds(Class)}, which returns an instance of
* {@link MultiIdentifierLoadAccess}.
*
* @param entityType the entity type
* @param ids the list of identifiers
* @param options options, if any
*
* @return an ordered list of persistent instances, with null elements representing missing
* entities, whose positions in the list match the positions of their ids in the
* given list of identifiers
* @see #byMultipleIds(Class)
* @since 7.0
*/
List findMultiple(Class entityType, List