#216 - ENH: Add Model.Finder deleteById(id) method

This commit is contained in:
rbygrave
2014-12-01 23:02:58 +13:00
parent 09f8967459
commit dd30b43d59
3 changed files with 38 additions and 5 deletions
+6 -4
View File
@@ -248,10 +248,12 @@ public abstract class Model {
return new Finder<I, T>(server, idType, type);
}
// Does not exist yet but I think should
// public void deleteById(I id) {
// return db().deleteById(type, id);
// }
/**
* Delete a bean by Id.
*/
public void deleteById(I id) {
db().delete(type, id);
}
/**
* Retrieves all entities of the given type.
@@ -455,7 +455,11 @@ public final class DefaultPersister implements Persister {
// delete the bean(s)
SqlUpdate deleteById = descriptor.deleteById(id, idList);
if (t.isLogSummary()) {
t.logSummary("-- Deleting " + descriptor.getName() + " Ids" + idList);
if (idList != null) {
t.logSummary("-- Deleting " + descriptor.getName() + " Ids: " + idList);
} else {
t.logSummary("-- Deleting " + descriptor.getName() + " Id: " + id);
}
}
// use Id's to update L2 cache rather than Bulk table event
@@ -0,0 +1,27 @@
package com.avaje.tests.delete;
import com.avaje.ebean.BaseTestCase;
import com.avaje.tests.model.onetoone.Account;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
public class TestDeleteByIdUsingModel extends BaseTestCase {
@Test
public void test() {
Account account = new Account();
account.setName("GoingToDeleteThis");
account.save();
Account account1 = Account.find.byId(account.getId());
assertEquals(account.getName(), account1.getName());
Account.find.deleteById(account.getId());
Account account2 = Account.find.byId(account.getId());
assertNull(account2);
}
}