mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#399 - ENH: Add new Finder ... to effectively replace Model.Finder
This commit is contained in:
@@ -5,6 +5,7 @@ import com.avaje.ebean.annotation.ChangeLogInsertMode;
|
||||
import com.avaje.ebean.annotation.EnumValue;
|
||||
import com.avaje.ebean.annotation.JsonIgnore;
|
||||
import com.avaje.ebean.annotation.Where;
|
||||
import com.avaje.tests.model.basic.finder.CustomerFinder;
|
||||
|
||||
import javax.persistence.CascadeType;
|
||||
import javax.persistence.Entity;
|
||||
@@ -29,6 +30,8 @@ public class Customer extends BasicDomain {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
public static final CustomerFinder find = new CustomerFinder();
|
||||
|
||||
/**
|
||||
* EnumValue is an Ebean specific mapping for enums.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.avaje.tests.model.basic.finder;
|
||||
|
||||
import com.avaje.ebean.Finder;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* Finder methods for Customer.
|
||||
*/
|
||||
public class CustomerFinder extends Finder<Integer,Customer> {
|
||||
|
||||
public CustomerFinder() {
|
||||
super(Customer.class);
|
||||
}
|
||||
|
||||
public CustomerFinder(String serverName) {
|
||||
super(Customer.class, serverName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find customer by unique name.
|
||||
*/
|
||||
@Nullable
|
||||
public Customer byName(String name) {
|
||||
|
||||
return query().where().eq("name", name).findUnique();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.avaje.tests.query.finder;
|
||||
|
||||
import com.avaje.ebean.BaseTestCase;
|
||||
import com.avaje.ebean.Ebean;
|
||||
import com.avaje.tests.model.basic.Customer;
|
||||
import com.avaje.tests.model.basic.ResetBasicData;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.StrictAssertions.assertThat;
|
||||
|
||||
public class TestCustomerFinder extends BaseTestCase {
|
||||
|
||||
@Test
|
||||
public void test_ref() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
Customer customer = Customer.find.ref(1);
|
||||
assertThat(customer.getId()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_all_byId_byName() {
|
||||
|
||||
ResetBasicData.reset();
|
||||
|
||||
List<Customer> all = Customer.find.all();
|
||||
List<Customer> list = Ebean.find(Customer.class).findList();
|
||||
|
||||
assertThat(all.size()).isEqualTo(list.size());
|
||||
|
||||
Customer customer = all.get(0);
|
||||
|
||||
Customer customer1 = Customer.find.byId(customer.getId());
|
||||
|
||||
assertThat(customer.getId()).isEqualTo(customer1.getId());
|
||||
assertThat(customer.getName()).isEqualTo(customer1.getName());
|
||||
|
||||
Customer customer2 = Customer.find.byName(customer.getName());
|
||||
assertThat(customer.getId()).isEqualTo(customer2.getId());
|
||||
assertThat(customer.getName()).isEqualTo(customer2.getName());
|
||||
|
||||
assertThat(Customer.find.db().getName()).isEqualTo(Ebean.getDefaultServer().getName());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_deleteById() {
|
||||
|
||||
Customer customer = new Customer();
|
||||
customer.setName("Newbie");
|
||||
|
||||
Ebean.save(customer);
|
||||
assertThat(customer.getId()).isNotNull();
|
||||
|
||||
Customer.find.deleteById(customer.getId());
|
||||
|
||||
Customer notThere = Customer.find.byId(customer.getId());
|
||||
assertThat(notThere).isNull();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user