diff --git a/src/main/java/io/ebean/Model.java b/src/main/java/io/ebean/Model.java index 9eae9b444..04f430091 100644 --- a/src/main/java/io/ebean/Model.java +++ b/src/main/java/io/ebean/Model.java @@ -192,6 +192,13 @@ public abstract class Model { db().save(this); } + /** + * Save this entity with an explicit transaction. + */ + public void save(Transaction transaction) { + db().save(this, transaction); + } + /** * Flush any batched changes to the database. *
@@ -212,6 +219,13 @@ public abstract class Model { db().update(this); } + /** + * Update this entity with an explicit transaction. + */ + public void update(Transaction transaction) { + db().update(this, transaction); + } + /** * Insert this entity. * @@ -221,6 +235,13 @@ public abstract class Model { db().insert(this); } + /** + * Insert with an explicit transaction. + */ + public void insert(Transaction transaction) { + db().insert(this, transaction); + } + /** * Delete this bean. *
@@ -242,6 +263,13 @@ public abstract class Model { return db().delete(this); } + /** + * Delete this entity with an explicit transaction. + */ + public boolean delete(Transaction transaction) { + return db().delete(this, transaction); + } + /** * Delete a bean permanently without soft delete. *
@@ -255,6 +283,13 @@ public abstract class Model { return db().deletePermanent(this); } + /** + * Delete a bean permanently without soft delete using an explicit transaction. + */ + public boolean deletePermanent(Transaction transaction) { + return db().deletePermanent(this, transaction); + } + /** * Refreshes this entity from the database. * diff --git a/src/test/java/org/tests/transaction/TestExplicitTransactionMode.java b/src/test/java/org/tests/transaction/TestExplicitTransactionMode.java index 23fa375d3..88dea2c50 100644 --- a/src/test/java/org/tests/transaction/TestExplicitTransactionMode.java +++ b/src/test/java/org/tests/transaction/TestExplicitTransactionMode.java @@ -1,6 +1,7 @@ package org.tests.transaction; import io.ebean.BaseTestCase; +import io.ebean.DB; import io.ebean.Database; import io.ebean.DatabaseFactory; import io.ebean.Query; @@ -131,4 +132,47 @@ public class TestExplicitTransactionMode extends BaseTestCase { super(JsonConfig.Date.ISO8601); } } + + @Test + public void modelSaveWithTransaction() { + + try (Transaction txn = DB.getDefault().createTransaction()) { + + UTMaster bean1 = new UTMaster("otherSave"); + bean1.save(txn); + assertThat(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherSave").findOne()).isNotNull(); + + bean1.deletePermanent(txn); + assertThat(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherSave").findOne()) + .isNull(); + + UTMaster bean2 = new UTMaster("otherInsert"); + bean2.insert(txn); + assertTrue(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherInsert").exists()); + + bean2.setDescription("changed description"); + bean2.update(txn); + + assertThat(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherInsert").findOne()) + .isNotNull() + .extracting(UTMaster::getDescription).contains("changed description"); + + UTMaster bean3 = new UTMaster("otherThree"); + bean3.save(txn); + + assertThat(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherThree").findOne()) + .isNotNull(); + + bean3.delete(txn); + + assertThat(DB.find(UTMaster.class).usingTransaction(txn) + .where().eq("name", "otherThree").findOne()) + .isNull(); + } + } }