HHH-9339 - java.lang.Optional support

This commit is contained in:
Steve Ebersole 2016-05-16 11:25:01 -05:00
parent aa9e1f0baf
commit bdff7ab169
3 changed files with 43 additions and 13 deletions

View File

@ -6,6 +6,9 @@
*/
package org.hibernate;
import java.io.Serializable;
import java.util.Optional;
/**
* Loads an entity by its natural identifier.
*
@ -22,7 +25,7 @@ public interface NaturalIdLoadAccess<T> {
*
* @return {@code this}, for method chaining
*/
public NaturalIdLoadAccess<T> with(LockOptions lockOptions);
NaturalIdLoadAccess<T> with(LockOptions lockOptions);
/**
* Add a NaturalId attribute value.
@ -32,7 +35,7 @@ public interface NaturalIdLoadAccess<T> {
*
* @return {@code this}, for method chaining
*/
public NaturalIdLoadAccess<T> using(String attributeName, Object value);
NaturalIdLoadAccess<T> using(String attributeName, Object value);
/**
* For entities with mutable natural ids, should Hibernate perform "synchronization" prior to performing
@ -50,7 +53,7 @@ public interface NaturalIdLoadAccess<T> {
*
* @return {@code this}, for method chaining
*/
public NaturalIdLoadAccess<T> setSynchronizationEnabled(boolean enabled);
NaturalIdLoadAccess<T> setSynchronizationEnabled(boolean enabled);
/**
* Return the persistent instance with the natural id value(s) defined by the call(s) to {@link #using}. This
@ -62,7 +65,7 @@ public interface NaturalIdLoadAccess<T> {
*
* @return the persistent instance or proxy
*/
public T getReference();
T getReference();
/**
* Return the persistent instance with the natural id value(s) defined by the call(s) to {@link #using}, or
@ -71,6 +74,14 @@ public interface NaturalIdLoadAccess<T> {
*
* @return The persistent instance or {@code null}
*/
public T load();
T load();
/**
* Same semantic as {@link #load} except that here {@link Optional} is returned to
* handle nullability.
*
* @return The persistent instance, if one, wrapped in Optional
*/
Optional<T> loadOptional();
}

View File

@ -6,6 +6,9 @@
*/
package org.hibernate;
import java.io.Serializable;
import java.util.Optional;
/**
* Loads an entity by its natural identifier.
*
@ -23,7 +26,7 @@ public interface SimpleNaturalIdLoadAccess<T> {
*
* @return {@code this}, for method chaining
*/
public SimpleNaturalIdLoadAccess<T> with(LockOptions lockOptions);
SimpleNaturalIdLoadAccess<T> with(LockOptions lockOptions);
/**
* For entities with mutable natural ids, should Hibernate perform "synchronization" prior to performing
@ -36,7 +39,7 @@ public interface SimpleNaturalIdLoadAccess<T> {
*
* @return {@code this}, for method chaining
*/
public SimpleNaturalIdLoadAccess<T> setSynchronizationEnabled(boolean enabled);
SimpleNaturalIdLoadAccess<T> setSynchronizationEnabled(boolean enabled);
/**
* Return the persistent instance with the given natural id value, assuming that the instance exists. This method
@ -50,7 +53,7 @@ public interface SimpleNaturalIdLoadAccess<T> {
*
* @return The persistent instance or proxy, if an instance exists. Otherwise, {@code null}.
*/
public T getReference(Object naturalIdValue);
T getReference(Object naturalIdValue);
/**
* Return the persistent instance with the given natural id value, or {@code null} if there is no such persistent
@ -61,6 +64,15 @@ public interface SimpleNaturalIdLoadAccess<T> {
*
* @return The persistent instance or {@code null}
*/
public T load(Object naturalIdValue);
T load(Object naturalIdValue);
/**
* Same semantic as {@link #load} except that here {@link Optional} is returned to
* handle nullability.
*
* @param naturalIdValue The identifier
*
* @return The persistent instance, if one, wrapped in Optional
*/
Optional<T> loadOptional(Serializable naturalIdValue);
}

View File

@ -3028,14 +3028,16 @@ public final class SessionImpl
try {
return (T) this.getIdentifierLoadAccess().load( entityId );
}
catch (EntityNotFoundException enf) {
// OK
}
catch (ObjectNotFoundException nf) {
catch (EntityNotFoundException | ObjectNotFoundException enf) {
// OK
}
return null;
}
@Override
public Optional<T> loadOptional() {
return Optional.ofNullable( load() );
}
}
private class SimpleNaturalIdLoadAccessImpl<T> extends BaseNaturalIdLoadAccessImpl<T>
@ -3106,6 +3108,11 @@ public final class SessionImpl
}
return null;
}
@Override
public Optional<T> loadOptional(Serializable naturalIdValue) {
return Optional.ofNullable( load( naturalIdValue ) );
}
}
@Override