#777 - ENH: Add support for @NamedQuery and @NamedQueries

This commit is contained in:
Robin Bygrave
2016-07-15 23:15:18 +12:00
parent cf13119b49
commit 05eb0bfc55
7 changed files with 96 additions and 12 deletions
@@ -68,4 +68,40 @@ public class EbeanServer_eqlTest extends BaseTestCase {
query.findUnique();
}
@Test
public void namedQuery() {
ResetBasicData.reset();
Query<Customer> name = server().createNamedQuery(Customer.class, "name");
name.findList();
assertThat(name.getGeneratedSql()).contains("select t0.id c0, t0.name c1 from o_customer t0 order by t0.name");
}
@Test
public void namedQuery_withStatus() {
ResetBasicData.reset();
Query<Customer> name = server().createNamedQuery(Customer.class, "withStatus");
name.order().clear().asc("status");
name.findList();
assertThat(name.getGeneratedSql()).contains("select t0.id c0, t0.name c1, t0.status c2 from o_customer t0 order by t0.status");
}
@Test
public void namedQuery_withContacts() {
ResetBasicData.reset();
Query<Customer> query = server()
.createNamedQuery(Customer.class, "withContacts")
.setParameter("id", 1);
query.findUnique();
assertThat(query.getGeneratedSql()).contains("from o_customer t0 left outer join contact t1 on t1.customer_id = t0.id ");
}
}
@@ -5,17 +5,8 @@ import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@Entity
@NamedQueries({
@NamedQuery(name="loadResult",
query="find CalculationResult " +
"fetch productConfiguration "+
"fetch groupConfiguration "+
"where charge = :charge")
})
public class CalculationResult {
@Id
@@ -14,6 +14,8 @@ import com.avaje.tests.model.basic.finder.CustomerFinder;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
@@ -27,6 +29,13 @@ import java.util.concurrent.locks.ReentrantLock;
/**
* Customer entity bean.
*/
@NamedQueries(
value = {
@NamedQuery(name = "name", query = "select(name) order by name"),
@NamedQuery(name = "withStatus", query = "select(name,status) order by name")
}
)
@NamedQuery(name="withContacts", query = "fetch contacts (firstName, lastName) where id = :id")
@Cache(enableQueryCache = true)
@DocStore
@ChangeLog(inserts = ChangeLogInsertMode.EXCLUDE, updatesThatInclude = {"name", "status"})