|
6 | 6 |
|
7 | 7 | import java.io.Serializable;
|
8 | 8 | import java.util.List;
|
| 9 | +import java.util.function.Consumer; |
| 10 | +import java.util.function.Function; |
9 | 11 |
|
10 | 12 | import jakarta.persistence.EntityGraph;
|
11 | 13 | import org.hibernate.graph.RootGraph;
|
|
15 | 17 | import org.hibernate.query.QueryProducer;
|
16 | 18 | import org.hibernate.query.criteria.HibernateCriteriaBuilder;
|
17 | 19 |
|
| 20 | +import static org.hibernate.internal.TransactionManagement.manageTransaction; |
| 21 | + |
18 | 22 | /**
|
19 | 23 | * Declares operations that are common between {@link Session} and {@link StatelessSession}.
|
20 | 24 | *
|
@@ -432,4 +436,31 @@ public interface SharedSessionContract extends QueryProducer, AutoCloseable, Ser
|
432 | 436 | * The factory which created this session.
|
433 | 437 | */
|
434 | 438 | SessionFactory getFactory();
|
| 439 | + |
| 440 | + /** |
| 441 | + * Perform an action within the bounds of a {@linkplain Transaction |
| 442 | + * transaction} associated with this session. |
| 443 | + * |
| 444 | + * @param action a void function which accepts the {@link Transaction} |
| 445 | + * |
| 446 | + * @since 7.0 |
| 447 | + */ |
| 448 | + default void inTransaction(Consumer<? super Transaction> action) { |
| 449 | + final Transaction transaction = beginTransaction(); |
| 450 | + manageTransaction( transaction, transaction, action ); |
| 451 | + } |
| 452 | + |
| 453 | + /** |
| 454 | + * Obtain a value within the bounds of a {@linkplain Transaction |
| 455 | + * transaction} associated with this session. |
| 456 | + * |
| 457 | + * @param action a function which accepts the {@link Transaction} and |
| 458 | + * returns the value |
| 459 | + * |
| 460 | + * @since 7.0 |
| 461 | + */ |
| 462 | + default <R> R fromTransaction(Function<? super Transaction,R> action) { |
| 463 | + final Transaction transaction = beginTransaction(); |
| 464 | + return manageTransaction( transaction, transaction, action ); |
| 465 | + } |
435 | 466 | }
|
0 commit comments