本文整理了Java中io.ebean.Query
类的一些代码示例,展示了Query
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query
类的具体详情如下:
包路径:io.ebean.Query
类名称:Query
[英]Object relational query for finding a List, Set, Map or single entity bean.
Example: Create the query using the API.
List orderList =
Example: The same query using the query language
String oql =
Example: Using a named query called "with.cust.and.details"
Query query = ebeanServer.createNamedQuery(Order.class,"with.cust.and.details");
Ebean has built in support for "AutoTune". This is a mechanism where a query can be automatically tuned based on profiling information that is collected.
This is effectively the same as automatically using select() and fetch() to build a query that will fetch all the data required by the application and no more.
It is expected that AutoTune will be the default approach for many queries in a system. It is possibly not as useful where the result of a query is sent to a remote client or where there is some requirement for "Read Consistency" guarantees.
Partial Objects
The find and fetch clauses support specifying a list of properties to fetch. This results in objects that are "partially populated". If you try to get a property that was not populated a "lazy loading" query will automatically fire and load the rest of the properties of the bean (This is very similar behaviour as a reference object being "lazy loaded").
Partial objects can be saved just like fully populated objects. If you do this you should remember to include the "Version" property in the initial fetch. If you do not include a version property then optimistic concurrency checking will occur but only include the fetched properties. Refer to "ALL Properties/Columns" mode of Optimistic Concurrency checking.
[ select [ ( * | {fetch properties} ) ] ]
[ fetch {path} [ ( * | {fetch properties} ) ] ]
[ where {predicates} ]
[ order by {order by properties} ]
[ limit {max rows} [ offset {first row} ] ]
}
SELECT [ ( * | {fetch properties} ) ]
With the select you can specify a list of properties to fetch.
FETCH{path} [ ( * | {fetch properties} ) ]
With the fetch you specify the associated property to fetch and populate. The path is a OneToOne, ManyToOne, OneToMany or ManyToMany property.
For fetch of a path we can optionally specify a list of properties to fetch. If you do not specify a list of properties ALL the properties for that bean type are fetched.
WHERE{list of predicates}
The list of predicates which are joined by AND OR NOT ( and ). They can include named (or positioned) bind parameters. These parameters will need to be bound by Query#setParameter(String,Object).
ORDER BY{order by properties}
The list of properties to order the result. You can include ASC (ascending) and DESC (descending) in the order by clause.
LIMIT{max rows} [ OFFSET {first row} ]
The limit offset specifies the max rows and first row to fetch. The offset is optional.
Find orders fetching its id, shipDate and status properties. Note that the id property is always fetched even if it is not included in the list of fetch properties.
select (shipDate, status)
Find orders with a named bind variable (that will need to be bound via Query#setParameter(String,Object)).
where customer.name like :custLike
Find orders and also fetch the customer with a named bind parameter. This will fetch and populate both the order and customer objects.
fetch customer
Find orders and also fetch the customer, customer shippingAddress, order details and related product. Note that customer and product objects will be "Partial Objects" with only some of their properties populated. The customer objects will have their id, name and shipping address populated. The product objects (associated with each order detail) will have their id, sku and name populated.
fetch customer (name)
[中]用于查找列表、集合、映射或单个实体bean的对象关系查询。
示例:使用API创建查询。
List orderList =
示例:使用查询语言的同一个查询
String oql =
示例:使用名为“with.cust.and.details”的命名查询
Query query = ebeanServer.createNamedQuery(Order.class,"with.cust.and.details");
####自动调谐
Ebean内置了对“自动调谐”的支持。这是一种可以根据收集的分析信息自动调整查询的机制。
这实际上与自动使用select()和fetch()来构建一个查询是一样的,该查询将获取应用程序所需的所有数据,而不是更多。
预计自动调谐将是系统中许多查询的默认方法。如果查询结果被发送到远程客户端,或者需要“读取一致性”保证,那么它可能没有那么有用。
####查询语言
部分对象
find和fetch子句支持指定要获取的属性列表。这会导致对象“部分填充”。如果试图获取未填充的属性,“延迟加载”查询将自动触发并加载bean的其余属性(这与“延迟加载”的引用对象的行为非常相似)。
部分对象可以像完全填充的对象一样保存。如果这样做,您应该记住在初始获取中包含*“Version”*属性。如果不包括版本属性,则将进行乐观并发检查,但只包括获取的属性。请参阅乐观并发检查的“所有属性/列”模式。
[ select [ ( * | {fetch properties} ) ] ]
[ fetch {path} [ ( * | {fetch properties} ) ] ]
[ where {predicates} ]
[ order by {order by properties} ]
[ limit {max rows} [ offset {first row} ] ]
}
选择[(*|{fetch properties}]
使用select可以指定要获取的属性列表。
FETCH{path}[(*|{FETCH properties}]
通过fetch,可以指定要获取和填充的关联属性。这条路是一个一通、多通、一通或多通的财产。
对于路径的获取,我们可以选择指定要获取的属性列表。如果没有指定属性列表,那么将获取该bean类型的所有属性。
其中{谓词列表}
由AND或NOT(AND)连接的谓词列表。它们可以包括命名(或定位)绑定参数。这些参数需要由查询#setParameter(字符串,对象)绑定。
按{按属性排序}
要对结果排序的属性列表。可以在order by子句中包含ASC(升序)和DESC(降序)。
限制{max rows}[偏移量{first row}]
限制偏移量指定要获取的最大行数和第一行数。偏移量是可选的。
#####Eben的查询语言示例
查找获取其id、发货日期和状态属性的订单。请注意,即使id属性未包含在获取属性列表中,也始终会获取它。
select (shipDate, status)
查找带有命名绑定变量(需要通过查询#setParameter(字符串,对象)绑定)的订单。
where customer.name like :custLike
查找订单,并使用命名绑定参数获取客户。这将获取并填充订单和客户对象。
fetch customer
查找订单,并获取客户、客户发货地址、订单详细信息和相关产品。请注意,customer和product对象将是“部分对象”,只填充了它们的一些属性。客户对象将填充其id、名称和发货地址。产品对象(与每个订单详细信息关联)将填充其id、sku和名称。
fetch customer (name)
代码示例来源:origin: ebean-orm/ebean
@Override
public int findCount() {
return query.findCount();
}
代码示例来源:origin: ebean-orm/ebean
@Override
public List<T> findList() {
return query.findList();
}
代码示例来源:origin: ebean-orm/ebean
@Override
public T findOne() {
return query.findOne();
}
代码示例来源:origin: ebean-orm/ebean
/**
* We need to create and execute a query to get the foreign key values as
* the delete cascades to them (foreign keys).
*/
private Query<?> deleteRequiresQuery(BeanDescriptor<?> desc, BeanPropertyAssocOne<?>[] propImportDelete, DeleteMode deleteMode) {
Query<?> q = server.createQuery(desc.getBeanType());
StringBuilder sb = new StringBuilder(30);
for (BeanPropertyAssocOne<?> aPropImportDelete : propImportDelete) {
sb.append(aPropImportDelete.getName()).append(",");
}
q.setAutoTune(false);
q.select(sb.toString());
if (deleteMode.isHard() && desc.isSoftDelete()) {
// hard delete so we want this query to include logically deleted rows (if any)
q.setIncludeSoftDeletes();
}
return q;
}
代码示例来源:origin: ebean-orm/ebean
/**
* Fetch the destination beans that will be published to.
*/
void fetchDestinationBeans(List<T> sourceBeans, boolean asDraft) {
List<Object> ids = getBeanIds(desc, sourceBeans);
Query<T> destQuery = server.find(desc.getBeanType()).where().idIn(ids).query();
if (asDraft) {
destQuery.asDraft();
}
desc.draftQueryOptimise(destQuery);
this.destBeans = server.findMap(destQuery, transaction);
}
代码示例来源:origin: io.github.hexagonframework.data/spring-data-ebean
@Override
public <S extends T> Page<S> findAll(Example<S> example, Pageable pageable) {
PagedList<S> pagedList = queryByExample(example)
.setMaxRows(pageable.getPageSize())
.setFirstRow((int) pageable.getOffset())
.setOrder(Converters.convertToEbeanOrderBy(pageable.getSort()))
.findPagedList();
return Converters.convertToSpringDataPage(pagedList, pageable.getSort());
}
代码示例来源:origin: icode/ameba
Object model;
if (includeDeleted) {
query.setIncludeSoftDeletes();
model = executeTx(t -> {
configureQuery.run(t);
List<MODEL> m = query.where().idIn(idCollection.toArray()).findList();
return processFoundByIdsModelList(m, includeDeleted);
});
model = executeTx(t -> {
configureQuery.run(t);
MODEL m = query.setId(firstId).findOne();
return processFoundByIdModel(m, includeDeleted);
});
代码示例来源:origin: io.github.hexagonframework.data/spring-data-ebean
@SuppressWarnings("unchecked")
<E> Page<E> findPage(Pageable pageable) {
if (queryType == QUERY) {
PagedList<E> pagedList = ((Query<E>) queryInstance)
.setFirstRow((int) pageable.getOffset())
.setMaxRows(pageable.getPageSize())
.findPagedList();
return PageableExecutionUtils.getPage(pagedList.getList(), pageable, pagedList::getTotalCount);
}
throw new IllegalArgumentException("query not supported!");
}
代码示例来源:origin: ebean-orm/ebean
@Override
public ExpressionList<T> where() {
return query.where();
}
代码示例来源:origin: io.github.hexagonframework.data/spring-data-ebean
@SuppressWarnings("unchecked")
<E> Slice<E> findSlice(Pageable pageable) {
List<E> resultList = null;
int pageSize = pageable.getPageSize();
int offset = (int) pageable.getOffset();
if (queryType == QUERY) {
resultList = ((Query<E>) queryInstance).setFirstRow(offset).setMaxRows(pageSize + 1).findList();
boolean hasNext = resultList.size() > pageSize;
return new SliceImpl<E>(hasNext ? resultList.subList(0, pageSize) : resultList, pageable, hasNext);
}
throw new IllegalArgumentException("query not supported!");
}
代码示例来源:origin: ebean-orm/ebean
return find(type).select(idProp.getName()).setId(id).findOne();
代码示例来源:origin: ebean-orm/ebean
@Override
public Query<T> apply(FetchPath fetchPath) {
return query.apply(fetchPath);
}
代码示例来源:origin: icode/ameba
/**
* find history as of timestamp
*
* @param id model id
* @param asOf Timestamp
* @return history model
* @throws java.lang.Exception any error
*/
public Response fetchHistoryAsOf(@PathParam("id") URI_ID id,
@PathParam("asof") final Timestamp asOf) throws Exception {
final MODEL_ID mId = tryConvertId(id);
matchedFetchHistoryAsOf(mId, asOf);
final Query<MODEL> query = server.find(modelType);
defaultFindOrderBy(query);
Object entity = executeTx(t -> {
configDefaultQuery(query);
configFetchHistoryAsOfQuery(query, mId, asOf);
applyUriQuery(query, false);
MODEL model = query.asOf(asOf).setId(mId).findOne();
return processFetchedHistoryAsOfModel(mId, model, asOf);
});
if (isEmptyEntity(entity)) {
return Response.noContent().build();
}
return Response.ok(entity).build();
}
代码示例来源:origin: io.github.hexagonframework.data/spring-data-ebean
@Override
public List<T> findAll(Sort sort) {
return query()
.setOrder(Converters.convertToEbeanOrderBy(sort))
.findList();
}
代码示例来源:origin: org.actframework/act-ebean
@Override
public MODEL_TYPE first() {
List<MODEL_TYPE> list = qReadOnly.setMaxRows(1).findList();
return list.isEmpty() ? null : list.get(0);
}
代码示例来源:origin: hexagonframework/spring-data-ebean
@Override
public void deletePermanentAll() {
query().setIncludeSoftDeletes().delete();
}
代码示例来源:origin: icode/ameba
query.setIncludeSoftDeletes();
configFindQuery(query, includeDeleted);
rowCount.set(applyUriQuery(query));
List<MODEL> list = query.findList();
return processFoundModelList(list, includeDeleted);
});
代码示例来源:origin: ebean-orm/ebean
@Override
public Query<T> setIncludeSoftDeletes() {
return query.setIncludeSoftDeletes();
}
代码示例来源:origin: ebean-orm/ebean
@Override
public Query<T> asOf(Timestamp asOf) {
return query.asOf(asOf);
}
代码示例来源:origin: ebean-orm/ebean
@Override
public Query<T> setMaxRows(int maxRows) {
return rootQuery.setMaxRows(maxRows);
}
内容来源于网络,如有侵权,请联系作者删除!