1212import java .util .ArrayList ;
1313import java .util .List ;
1414import java .util .Optional ;
15+ import java .util .function .Consumer ;
16+ import java .util .function .Function ;
1517import java .util .logging .Logger ;
1618
1719/**
@@ -108,11 +110,8 @@ public T save(T entity) {
108110 throw new IllegalArgumentException ("Entity must not be null" );
109111 }
110112
111- try (Session session = sessionFactory .openSession ()) {
112- Transaction transaction = session .beginTransaction ();
113- T savedEntity = session .merge (entity );
114- transaction .commit ();
115- return savedEntity ;
113+ try {
114+ return executeInTransaction (session -> session .merge (entity ));
116115 } catch (Exception e ) {
117116 logger .severe ("Error saving entity: " + e .getMessage ());
118117 throw new RuntimeException ("Failed to save entity" , e );
@@ -152,11 +151,8 @@ public Optional<T> findById(ID id) {
152151 throw new IllegalArgumentException ("ID must not be null" );
153152 }
154153
155- try (Session session = sessionFactory .openSession ()) {
156- Transaction transaction = session .beginTransaction ();
157- T entity = session .get (entityClass , id );
158- transaction .commit ();
159- return Optional .ofNullable (entity );
154+ try {
155+ return executeInTransaction (session -> Optional .ofNullable (session .get (entityClass , id )));
160156 } catch (Exception e ) {
161157 logger .severe ("Error finding entity by ID: " + e .getMessage ());
162158 throw new RuntimeException ("Failed to find entity" , e );
@@ -244,13 +240,13 @@ public void deleteById(ID id) {
244240 throw new IllegalArgumentException ("ID must not be null" );
245241 }
246242
247- try ( Session session = sessionFactory . openSession ()) {
248- Transaction transaction = session . beginTransaction ();
249- T entity = session .get (entityClass , id );
250- if (entity != null ) {
251- session .remove (entity );
252- }
253- transaction . commit ( );
243+ try {
244+ executeInTransactionVoid ( session -> {
245+ T entity = session .get (entityClass , id );
246+ if (entity != null ) {
247+ session .remove (entity );
248+ }
249+ } );
254250 logger .fine ("Deleted entity with ID: " + id );
255251 } catch (Exception e ) {
256252 logger .severe ("Error deleting entity by ID: " + e .getMessage ());
@@ -290,7 +286,7 @@ public void deleteAllById(Iterable<ID> ids) {
290286
291287 try (Session session = sessionFactory .openSession ()) {
292288 Transaction transaction = session .beginTransaction ();
293- Query <?> query = session .createQuery ( // FIXME: deprecated method
289+ var query = session .createMutationQuery (
294290 "DELETE FROM " + entityName + " e WHERE e." + idFieldName + " IN (:ids)" );
295291 query .setParameterList ("ids" , idList );
296292 int deletedCount = query .executeUpdate ();
@@ -329,7 +325,7 @@ public void deleteAll(Iterable<T> entities) {
329325 public void deleteAll () {
330326 try (Session session = sessionFactory .openSession ()) {
331327 Transaction transaction = session .beginTransaction ();
332- Query <?> query = session .createQuery ("DELETE FROM " + entityName ); // FIXME: deprecated method
328+ var query = session .createMutationQuery ("DELETE FROM " + entityName );
333329 int deletedCount = query .executeUpdate ();
334330 transaction .commit ();
335331 logger .fine ("Deleted all " + deletedCount + " entities of type: " + entityName );
@@ -386,4 +382,47 @@ public void deleteInBatch(Iterable<T> entities) {
386382 public void deleteAllInBatch () {
387383 deleteAll ();
388384 }
385+
386+ /**
387+ * Helper method to execute operations in a transaction with proper rollback handling.
388+ *
389+ * @param operation the operation to execute
390+ * @param <R> the return type
391+ * @return the result of the operation
392+ */
393+ protected <R > R executeInTransaction (Function <Session , R > operation ) {
394+ try (Session session = sessionFactory .openSession ()) {
395+ Transaction transaction = session .beginTransaction ();
396+ try {
397+ R result = operation .apply (session );
398+ transaction .commit ();
399+ return result ;
400+ } catch (Exception e ) {
401+ if (transaction .isActive ()) {
402+ transaction .rollback ();
403+ }
404+ throw e ;
405+ }
406+ }
407+ }
408+
409+ /**
410+ * Helper method to execute void operations in a transaction with proper rollback handling.
411+ *
412+ * @param operation the operation to execute
413+ */
414+ protected void executeInTransactionVoid (Consumer <Session > operation ) {
415+ try (Session session = sessionFactory .openSession ()) {
416+ Transaction transaction = session .beginTransaction ();
417+ try {
418+ operation .accept (session );
419+ transaction .commit ();
420+ } catch (Exception e ) {
421+ if (transaction .isActive ()) {
422+ transaction .rollback ();
423+ }
424+ throw e ;
425+ }
426+ }
427+ }
389428}
0 commit comments