@Override public <T> T publish(Class<T> beanType, Object id, Transaction transaction) { Query<T> query = find(beanType).setId(id); List<T> liveBeans = publish(query, transaction); return (liveBeans.size() == 1) ? liveBeans.get(0) : null; }
@Override public <T> T draftRestore(Class<T> beanType, Object id, Transaction transaction) { Query<T> query = find(beanType).setId(id); List<T> beans = draftRestore(query, transaction); return (beans.size() == 1) ? beans.get(0) : null; }
@Override public boolean exists(Class<?> beanType, Object beanId, Transaction transaction) { List<Object> ids = findIds(find(beanType).setId(beanId), transaction); return !ids.isEmpty(); }
@Override public ExpressionList<T> idEq(Object value) { if (query != null && parentExprList == null) { query.setId(value); } else { add(expr.idEq(value)); } return this; }
/** * Find an entity by ID returning an Optional. */ @Nullable public Optional<T> findByIdOrEmpty(I id) { return db().find(type).setId(id).findOneOrEmpty(); }
@Override public Set<Property> checkUniqueness(Object bean, Transaction transaction) { EntityBean entityBean = checkEntityBean(bean); BeanDescriptor<?> beanDesc = getBeanDescriptor(entityBean.getClass()); BeanProperty idProperty = beanDesc.getIdProperty(); // if the ID of the Property is null we are unable to check uniqueness if (idProperty == null) { return Collections.emptySet(); } Object id = idProperty.getVal(entityBean); if (entityBean._ebean_getIntercept().isNew() && id != null) { // Primary Key is changeable only on new models - so skip check if we are not // new. Query<?> query = new DefaultOrmQuery<>(beanDesc, this, expressionFactory); query.setId(id); if (findCount(query, transaction) > 0) { Set<Property> ret = new HashSet<>(); ret.add(idProperty); return ret; } } for (BeanProperty[] props : beanDesc.getUniqueProps()) { Set<Property> ret = checkUniqueness(entityBean, beanDesc, props, transaction); if (ret != null) { return ret; } } return Collections.emptySet(); }
@Override public EbeanQuery<MODEL_TYPE> setId(Object id) { q.setId(id); return this; }
/** * Fetch the outline bean with associated one and associated many beans loaded with Id values only. * <p> * We use the Id values to determine what are inserts, updates and deletes as part of the merge. */ private EntityBean fetchOutline(Set<String> paths) { Query<?> query = server.find(desc.getBeanType()); query.setBeanCacheMode(CacheMode.OFF); query.setPersistenceContextScope(PersistenceContextScope.QUERY); query.setId(desc.getId(bean)); query.select(desc.getIdProperty().getName()); for (String path : paths) { MergeNode node = buildNode(path); node.addSelectId(query); } return (EntityBean) server.findOne(query, transaction); }
/** * Set the Id value to query. This is used with findOne(). * <p> * You can use this to have further control over the query. For example adding * fetch joins. * </p> * <p> * <pre>{@code * * Order order = * new QOrder() * .setId(1) * .fetch("details") * .findOne(); * * // the order details were eagerly fetched * List<OrderDetail> details = order.getDetails(); * * }</pre> */ public R setId(Object id) { query.setId(id); return root; }
return find(type).select(idProp.getName()).setId(id).findOne();
/** * {@inheritDoc} * Sets the ID value to query. */ public Query<T> setId(Object id) { return query().setId(id); }
@Override public <T> T publish(Class<T> beanType, Object id, Transaction transaction) { Query<T> query = find(beanType).setId(id); List<T> liveBeans = publish(query, transaction); return (liveBeans.size() == 1) ? liveBeans.get(0) : null; }
@Override public <T> T draftRestore(Class<T> beanType, Object id, Transaction transaction) { Query<T> query = find(beanType).setId(id); List<T> beans = draftRestore(query, transaction); return (beans.size() == 1) ? beans.get(0) : null; }
@Override public boolean exists(Class<?> beanType, Object beanId, Transaction transaction) { List<Object> ids = findIds(find(beanType).setId(beanId), transaction); return !ids.isEmpty(); }
@Override public ExpressionList<T> idEq(Object value) { if (query != null && parentExprList == null) { query.setId(value); } else { add(expr.idEq(value)); } return this; }
/** * Find an entity by ID returning an Optional. */ @Nullable public Optional<T> findByIdOrEmpty(I id) { return db().find(type).setId(id).findOneOrEmpty(); }
@Override public Set<Property> checkUniqueness(Object bean, Transaction transaction) { EntityBean entityBean = checkEntityBean(bean); BeanDescriptor<?> beanDesc = getBeanDescriptor(entityBean.getClass()); BeanProperty idProperty = beanDesc.getIdProperty(); // if the ID of the Property is null we are unable to check uniqueness if (idProperty == null) { return Collections.emptySet(); } Object id = idProperty.getVal(entityBean); if (entityBean._ebean_getIntercept().isNew() && id != null) { // Primary Key is changeable only on new models - so skip check if we are not // new. Query<?> query = new DefaultOrmQuery<>(beanDesc, this, expressionFactory); query.setId(id); if (findCount(query, transaction) > 0) { Set<Property> ret = new HashSet<>(); ret.add(idProperty); return ret; } } for (BeanProperty[] props : beanDesc.getUniqueProps()) { Set<Property> ret = checkUniqueness(entityBean, beanDesc, props, transaction); if (ret != null) { return ret; } } return Collections.emptySet(); }
/** * 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(); }
/** * Fetch the outline bean with associated one and associated many beans loaded with Id values only. * <p> * We use the Id values to determine what are inserts, updates and deletes as part of the merge. */ private EntityBean fetchOutline(Set<String> paths) { Query<?> query = server.find(desc.getBeanType()); query.setBeanCacheMode(CacheMode.OFF); query.setPersistenceContextScope(PersistenceContextScope.QUERY); query.setId(desc.getId(bean)); query.select(desc.getIdProperty().getName()); for (String path : paths) { MergeNode node = buildNode(path); node.addSelectId(query); } return (EntityBean) server.findOne(query, transaction); }