mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#1934 - ENH: Add methods to Model that take explicit transaction - save(transaction), delete(transaction) ...
This commit is contained in:
@@ -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.
|
||||
* <p>
|
||||
@@ -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.
|
||||
* <p>
|
||||
@@ -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.
|
||||
* <p>
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user