Top Description Methods
org.hibernate

public Interface NaturalIdLoadAccess<T>

Known Direct Implementers
org.hibernate.loader.internal.NaturalIdLoadAccessImpl
Imports
jakarta.persistence.metamodel.SingularAttribute, org.hibernate.graph.GraphSemantic, .RootGraph, java.util.Map, .Optional

Loads an entity by its natural identifier, which may be a composite value comprising more than one attribute of the entity. If the entity has exactly one attribute annotated @NaturalId, then SimpleNaturalIdLoadAccess may be used instead.

Book book =
        session.byNaturalId(Book.class)
            .using(Book_.isbn, isbn)
            .using(Book_.printing, printing)
            .load();
Authors
Eric Dalquist, Steve Ebersole
See Also
Session#byNaturalId(Class), org.hibernate.annotations.NaturalId, SimpleNaturalIdLoadAccess

Method Summary

Modifier and TypeMethod and Description
public NaturalIdLoadAccess<T>
disableFetchProfile(String profileName)

Customize the associations fetched by specifying a fetch profile that should be disabled during this operation.

public NaturalIdLoadAccess<T>
enableFetchProfile(String profileName)

Customize the associations fetched by specifying a fetch profile that should be enabled during this operation.

public T

Returns:

the persistent instance or proxy
getReference
()

Return the persistent instance with the full natural id specified by previous calls to using.

public T

Returns:

The persistent instance or null
load
()

Return the persistent instance with the full natural id specified by previous calls to using, or null if there is no such persistent instance.

public Optional<T>

Returns:

The persistent instance, if one, as an Optional
loadOptional
()

Just like load, except that here an Optional is returned.

public NaturalIdLoadAccess<T>

Returns:

this, for method chaining
setSynchronizationEnabled
(boolean
Should synchronization be performed? true indicates synchronization will be performed; false indicates it will be circumvented.
enabled
)

Determines if cached natural id cross-references are synchronized before query execution with unflushed modifications made in memory to mutable natural ids.

public <X> NaturalIdLoadAccess<T>

Returns:

this, for method chaining
using
(SingularAttribute<? super T, X>
A typesafe reference to an attribute of the entity that is annotated @NaturalId
attribute
,
X
The value of the attribute
value
)

Add a @NaturalId attribute value in a typesafe way.

public NaturalIdLoadAccess<T>

Returns:

this, for method chaining
using
(String
The name of an attribute of the entity that is annotated @NaturalId
attributeName
,
Object
The value of the attribute
value
)

Add a @NaturalId attribute value.

public NaturalIdLoadAccess<T>

Returns:

this, for method chaining
using
(Map<String, ?> mappings)

Set multiple @NaturalId attribute values at once.

public NaturalIdLoadAccess<T>

Returns:

this, for method chaining
using
(Object... mappings)

Deprecated since 6.3. use using(Map) with Map#of, which is slightly more typesafe
Set multiple @NaturalId attribute values at once.
public NaturalIdLoadAccess<T>

Returns:

this, for method chaining
with
(LockOptions
The lock options to use.
lockOptions
)

Specify the lock options to use when querying the database.

public NaturalIdLoadAccess<T>
with(RootGraph<T> graph, GraphSemantic semantic)

Customize the associations fetched by specifying an entity graph, and how it should be interpreted.

public default NaturalIdLoadAccess<T>
withFetchGraph(RootGraph<T> graph)

Override the associations fetched by default by specifying the complete list of associations to be fetched as an entity graph.

public default NaturalIdLoadAccess<T>
withLoadGraph(RootGraph<T> graph)

Augment the associations fetched by default by specifying a list of additional associations to be fetched as an entity graph.

Method Detail

disableFetchProfileback to summary
public NaturalIdLoadAccess<T> disableFetchProfile(String profileName)

Customize the associations fetched by specifying a fetch profile that should be disabled during this operation.

This allows the session-level fetch profiles to be temporarily overridden.

Since
6.3
enableFetchProfileback to summary
public NaturalIdLoadAccess<T> enableFetchProfile(String profileName)

Customize the associations fetched by specifying a fetch profile that should be enabled during this operation.

This allows the session-level fetch profiles to be temporarily overridden.

Since
6.3
getReferenceback to summary
public T getReference()

Return the persistent instance with the full natural id specified by previous calls to using. This method might return a proxied instance that is initialized on-demand, when a non-identifier method is accessed.

You should not use this method to determine if an instance exists; to check for existence, use load instead. Use this method only to retrieve an instance that you assume exists, where non-existence would be an actual error.

Returns:T

the persistent instance or proxy

loadback to summary
public T load()

Return the persistent instance with the full natural id specified by previous calls to using, or null if there is no such persistent instance. If the instance is already associated with the session, return that instance, initializing it if needed. This method never returns an uninitialized instance.

Returns:T

The persistent instance or null

loadOptionalback to summary
public Optional<T> loadOptional()

Just like load, except that here an Optional is returned.

Returns:Optional<T>

The persistent instance, if one, as an Optional

setSynchronizationEnabledback to summary
public NaturalIdLoadAccess<T> setSynchronizationEnabled(boolean enabled)

Determines if cached natural id cross-references are synchronized before query execution with unflushed modifications made in memory to mutable natural ids.

By default, every cached cross-reference is updated to reflect any modification made in memory.

Here "synchronization" means updating the natural id to primary key cross-reference maintained by the session. When enabled, before performing the lookup, Hibernate will check all entities associated with the session of the given type to determine if any natural id values have changed and, if so, update the cross-references.

There's some cost associated with this synchronization, so if it's completely certain the no natural ids have been modified, synchronization may be safely disabled to avoid that cost. Disabling this setting when natural id values have been modified may lead to incorrect results.

Parameters
enabled:boolean

Should synchronization be performed? true indicates synchronization will be performed; false indicates it will be circumvented.

Returns:NaturalIdLoadAccess<T>

this, for method chaining

usingback to summary
public <X> NaturalIdLoadAccess<T> using(SingularAttribute<? super T, X> attribute, X value)

Add a @NaturalId attribute value in a typesafe way.

Parameters
attribute:SingularAttribute<? super T, X>

A typesafe reference to an attribute of the entity that is annotated @NaturalId

value:X

The value of the attribute

Returns:NaturalIdLoadAccess<T>

this, for method chaining

usingback to summary
public NaturalIdLoadAccess<T> using(String attributeName, Object value)

Add a @NaturalId attribute value.

Parameters
attributeName:String

The name of an attribute of the entity that is annotated @NaturalId

value:Object

The value of the attribute

Returns:NaturalIdLoadAccess<T>

this, for method chaining

usingback to summary
public NaturalIdLoadAccess<T> using(Map<String, ?> mappings)

Set multiple @NaturalId attribute values at once. An even number of arguments is expected, with each attribute name followed by its value, for example:

Book book =
        session.byNaturalId(Book.class)
            .using(Map.of(Book_.ISBN, isbn, Book_.PRINTING, printing))
            .load();
Returns:NaturalIdLoadAccess<T>

this, for method chaining

usingback to summary
public NaturalIdLoadAccess<T> using(Object... mappings)

Deprecated

since 6.3.

use using(Map) with Map#of, which is slightly more typesafe

Set multiple @NaturalId attribute values at once. An even number of arguments is expected, with each attribute name followed by its value, for example:

Book book =
        session.byNaturalId(Book.class)
            .using(Book_.ISBN, isbn, Book_.PRINTING, printing)
            .load();
Returns:NaturalIdLoadAccess<T>

this, for method chaining

Annotations
@Deprecated
since:6.3
withback to summary
public NaturalIdLoadAccess<T> with(LockOptions lockOptions)

Specify the lock options to use when querying the database.

Parameters
lockOptions:LockOptions

The lock options to use.

Returns:NaturalIdLoadAccess<T>

this, for method chaining

withback to summary
public NaturalIdLoadAccess<T> with(RootGraph<T> graph, GraphSemantic semantic)

Customize the associations fetched by specifying an entity graph, and how it should be interpreted.

withFetchGraphback to summary
public default NaturalIdLoadAccess<T> withFetchGraph(RootGraph<T> graph)

Override the associations fetched by default by specifying the complete list of associations to be fetched as an entity graph.

withLoadGraphback to summary
public default NaturalIdLoadAccess<T> withLoadGraph(RootGraph<T> graph)

Augment the associations fetched by default by specifying a list of additional associations to be fetched as an entity graph.