Skip to content

Commit 77160a8

Browse files
committed
add a cute API to propagate scoped typed objects with the PC
1 parent ed05cdf commit 77160a8

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

hibernate-core/src/main/java/org/hibernate/Session.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,36 @@ public interface Session extends SharedSessionContract, EntityManager {
14521452
@Override
14531453
void setProperty(String propertyName, Object value);
14541454

1455+
/**
1456+
* Set a property whose key is the type of the value.
1457+
* The value may later be retrieved by calling
1458+
* {@link #getProperty(Class)}. If there is already
1459+
* a property value of the same type, overwrite it.
1460+
*
1461+
* @apiNote The application program or framework may
1462+
* use this mechanism to register an application or
1463+
* framework object whose scope is somehow tied to
1464+
* the persistence context.
1465+
*
1466+
* @param value A non-null Java object
1467+
*
1468+
* @since 4.0
1469+
*/
1470+
@Incubating
1471+
void setProperty(Object value);
1472+
1473+
/**
1474+
* Retrieve a property value set by a previous call to
1475+
* {@link #setProperty(Object)}.
1476+
* @param type The type of the property value
1477+
* @return The property value, or null if no property
1478+
* value of the given type was set
1479+
*
1480+
* @since 4.0
1481+
*/
1482+
@Incubating
1483+
<T> T getProperty(Class<T> type);
1484+
14551485
/**
14561486
* Create a new mutable instance of {@link EntityGraph}, with only
14571487
* a root node, allowing programmatic definition of the graph from

hibernate-core/src/main/java/org/hibernate/internal/SessionImpl.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,24 @@ public LockModeType getLockMode(Object entity) {
25792579

25802580
}
25812581

2582+
@Override
2583+
public void setProperty(Object value) {
2584+
if ( value == null ) {
2585+
throw new IllegalArgumentException( "Property value may not be null" );
2586+
}
2587+
setProperty( value.getClass().getName(), value );
2588+
}
2589+
2590+
@Override
2591+
public <T> T getProperty(Class<T> type) {
2592+
final var value = getProperties().get( type.getName() );
2593+
if ( !type.isInstance( value ) ) {
2594+
throw new IllegalArgumentException( "Property value is not of expected type: " + type.getName() );
2595+
}
2596+
// noinspection unchecked
2597+
return (T) value;
2598+
}
2599+
25822600
@Override
25832601
public void setProperty(String propertyName, Object value) {
25842602
checkOpen();

0 commit comments

Comments
 (0)