Skip to content

Commit c18ddc7

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

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2579,6 +2579,34 @@ 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+
if ( properties == null ) {
2593+
return null;
2594+
}
2595+
else {
2596+
final var value = properties.get( type.getName() );
2597+
if ( value == null ) {
2598+
return null;
2599+
}
2600+
else if ( type.isInstance( value ) ) {
2601+
// noinspection unchecked
2602+
return (T) value;
2603+
}
2604+
else {
2605+
throw new IllegalArgumentException( "Property value is not of expected type: " + type.getName() );
2606+
}
2607+
}
2608+
}
2609+
25822610
@Override
25832611
public void setProperty(String propertyName, Object value) {
25842612
checkOpen();

0 commit comments

Comments
 (0)