#1271 - ENH: Add BeanFinder and BeanRepository ... such that there is a read to go base class for DI repository style

This commit is contained in:
Rob Bygrave
2018-02-23 16:55:07 +13:00
parent ff111eaf33
commit 7d0ed261b5
4 changed files with 444 additions and 0 deletions
@@ -0,0 +1,36 @@
package org.tests.repository;
import io.ebean.BeanRepository;
import io.ebean.EbeanServer;
import org.tests.model.basic.Customer;
import javax.inject.Inject;
import java.util.List;
public class CustomerRepository extends BeanRepository<Integer, Customer> {
@Inject
public CustomerRepository(EbeanServer server) {
super(Customer.class, server);
}
public List<Customer> findByName(String nameStart) {
return query().where()
.istartsWith("name", nameStart)
.findList();
}
public Customer findMatchName(String matchName) {
return nativeSql("select id, name from o_customer where name = ?")
.setParameter(1, matchName)
.findOne();
}
public int updateNotes(String blah, String whot) {
return updateQuery()
.set("smallnote", whot)
.where().eq("name", blah)
.update();
}
}
@@ -0,0 +1,61 @@
package org.tests.repository;
import io.ebean.BaseTestCase;
import org.junit.Test;
import org.tests.model.basic.Customer;
import java.util.List;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
public class TestBeanRepository extends BaseTestCase {
@Test
public void test() {
CustomerRepository repository = new CustomerRepository(server());
Customer customer = new Customer();
customer.setName("RepoCustomer");
repository.save(customer);
Customer fetchCustomer = repository.findById(customer.getId());
fetchCustomer.setSmallnote("yeah maybe");
repository.update(fetchCustomer);
repository.delete(fetchCustomer);
}
@Test
public void findByName() {
CustomerRepository repository = new CustomerRepository(server());
Customer blah = new Customer();
blah.setName("Blah");
repository.markAsDirty(blah);
repository.markPropertyUnset(blah, "smallnote");
repository.save(blah);
Optional<Customer> maybe = repository.findByIdOrEmpty(blah.getId());
assertThat(maybe.isPresent()).isTrue();
List<Customer> names = repository.findByName("bla");
assertThat(names).hasSize(1);
Customer matchName = repository.findMatchName("Blah");
assertThat(matchName).isNotNull();
int rows = repository.updateNotes("Blah", "whot");
assertThat(rows).isEqualTo(1);
repository.deletePermanent(blah);
repository.deleteById(1099);
repository.findAll();
}
}