Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions hibernate-core/src/main/java/org/hibernate/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,36 @@ public interface Session extends SharedSessionContract, EntityManager {
@Override
void setProperty(String propertyName, Object value);

/**
* Set a property whose key is the type of the value.
* The value may later be retrieved by calling
* {@link #getProperty(Class)}. If there is already
* a property value of the same type, overwrite it.
*
* @apiNote The application program or framework may
* use this mechanism to register an application or
* framework object whose scope is somehow tied to
* the persistence context.
*
* @param value A non-null Java object
*
* @since 4.0
*/
@Incubating
void setProperty(Object value);

/**
* Retrieve a property value set by a previous call to
* {@link #setProperty(Object)}.
* @param type The type of the property value
* @return The property value, or null if no property
* value of the given type was set
*
* @since 4.0
*/
@Incubating
<T> T getProperty(Class<T> type);

/**
* Create a new mutable instance of {@link EntityGraph}, with only
* a root node, allowing programmatic definition of the graph from
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2579,6 +2579,34 @@ public LockModeType getLockMode(Object entity) {

}

@Override
public void setProperty(Object value) {
if ( value == null ) {
throw new IllegalArgumentException( "Property value may not be null" );
}
setProperty( value.getClass().getName(), value );
}

@Override
public <T> T getProperty(Class<T> type) {
if ( properties == null ) {
return null;
}
else {
final var value = properties.get( type.getName() );
if ( value == null ) {
return null;
}
else if ( type.isInstance( value ) ) {
// noinspection unchecked
return (T) value;
}
else {
throw new IllegalArgumentException( "Property value is not of expected type: " + type.getName() );
}
}
}

@Override
public void setProperty(String propertyName, Object value) {
checkOpen();
Expand Down
Loading