@Override public ScalarDataReader<?> getIdReader() { return targetDescriptor.getIdProperty(); }
/** * Add to the query to fetch the Ids values for the foreign keys basically. */ void addSelectId(Query<?> query) { BeanProperty idProperty = targetDescriptor.getIdProperty(); query.fetch(fullPath, idProperty.getName()); }
@Override public void appendSelect(DbSqlContext ctx, boolean subQuery) { if (!isTransient) { if (primaryKeyExport) { descriptor.getIdProperty().appendSelect(ctx, subQuery); } else { localHelp.appendSelect(ctx, subQuery); } } }
/** * Return true if the unique id properties are all not null for this bean. */ public boolean hasId(EntityBean bean) { BeanDescriptor<?> targetDesc = getTargetDescriptor(); BeanProperty idProp = targetDesc.getIdProperty(); if (idProp != null) { Object value = idProp.getValue(bean); if (value == null) { return false; } } // all the unique properties are non-null return true; }
/** * Add a PathProperties for an embedded 'many' property (at the root level). */ private void prepare(String prop, BeanPropertyAssoc<?> embProp) { BeanDescriptor<?> targetDesc = embProp.getTargetDescriptor(); PathProperties manyRootPath = new PathProperties(); manyRootPath.addToPath(null, targetDesc.getIdProperty().getName()); manyRootPath.addNested(prop, embedded.get(prop)); manyRoot.put(prop, manyRootPath); } }
@Override public Object getCacheDataValue(EntityBean bean) { Object ap = getValue(bean); if (ap == null) { return null; } if (embedded) { return targetDescriptor.cacheEmbeddedBeanExtract((EntityBean) ap); } else { return targetDescriptor.getIdProperty().getCacheDataValue((EntityBean) ap); } }
/** * Just write the Id property of the ToOne property. */ private void jsonWriteTargetId(SpiJsonWriter writeJson, EntityBean childBean) throws IOException { BeanProperty idProperty = targetDescriptor.getIdProperty(); if (idProperty != null) { writeJson.writeStartObject(name); idProperty.jsonWriteForInsert(writeJson, childBean); writeJson.writeEndObject(); } }
/** * Initialise after the target bean descriptors have been all set. */ public void initialisePostTarget() { if (childMasterProperty != null) { BeanProperty masterId = childMasterProperty.getTargetDescriptor().getIdProperty(); if (masterId != null) { // in docstore only, the master-id may be not available childMasterIdProperty = childMasterProperty.getName() + "." + masterId.getName(); } } }
private void buildExport(IntersectionRow row, EntityBean parentBean) { if (embeddedExportedProperties) { BeanProperty idProp = descriptor.getIdProperty(); parentBean = (EntityBean) idProp.getValue(parentBean); } for (ExportedProperty exportedProperty : exportedProperties) { Object val = exportedProperty.getValue(parentBean); String fkColumn = exportedProperty.getForeignDbColumn(); row.put(fkColumn, val); } }
/** * Bind the two side of a many to many to the given SqlUpdate. */ public void intersectionBind(SqlUpdate sql, EntityBean parentBean, EntityBean other) { if (embeddedExportedProperties) { BeanProperty idProp = descriptor.getIdProperty(); parentBean = (EntityBean) idProp.getValue(parentBean); } for (ExportedProperty exportedProperty : exportedProperties) { sql.setNextParameter(exportedProperty.getValue(parentBean)); } importedId.bindImport(sql, other); }
/** * Execute the query as findMap. */ @Override @SuppressWarnings("unchecked") public <K> Map<K, T> findMap() { String mapKey = query.getMapKey(); if (mapKey == null) { BeanProperty idProp = beanDescriptor.getIdProperty(); if (idProp != null) { query.setMapKey(idProp.getName()); } else { throw new PersistenceException("No mapKey specified for query"); } } return (Map<K, T>) queryEngine.findMany(this); }
/** * Add uniqueId properties. */ public BindableId createId(BeanDescriptor<?> desc) { BeanProperty id = desc.getIdProperty(); if (id == null) { return new BindableIdEmpty(); } if (!id.isEmbedded()) { return new BindableIdScalar(id); } else { BeanPropertyAssocOne<?> embId = (BeanPropertyAssocOne<?>) id; MatchedImportedProperty[] matches = MatchedImportedFactory.build(embId.getProperties(), desc); return new BindableIdEmbedded(embId, matches); } } }
/** * Returns a set of properties if saving the bean will violate the unique constraints (defined by given properties). */ private Set<Property> checkUniqueness(EntityBean entityBean, BeanDescriptor<?> beanDesc, BeanProperty[] props, Transaction transaction) { BeanProperty idProperty = beanDesc.getIdProperty(); Query<?> query = new DefaultOrmQuery<>(beanDesc, this, expressionFactory); ExpressionList<?> exprList = query.where(); if (!entityBean._ebean_getIntercept().isNew()) { // if model is not new, exclude ourself. exprList.ne(idProperty.getName(), idProperty.getVal(entityBean)); } for (Property prop : props) { Object value = prop.getVal(entityBean); if (value == null) { return null; } exprList.eq(prop.getName(), value); } if (findCount(query, transaction) > 0) { Set<Property> ret = new LinkedHashSet<>(); Collections.addAll(ret, props); return ret; } return null; }
@Override public void setCacheDataValue(EntityBean bean, Object cacheData, PersistenceContext context) { if (cacheData == null) { setValue(bean, null); } else { if (embedded) { setValue(bean, targetDescriptor.cacheEmbeddedBeanLoad((CachedBeanData) cacheData, context)); } else { if (cacheData instanceof String) { cacheData = targetDescriptor.getIdProperty().scalarType.parse((String) cacheData); } // cacheData is the id value, maybe already in persistence context Object assocBean = targetDescriptor.contextGet(context, cacheData); if (assocBean == null) { assocBean = targetDescriptor.createReference(cacheData, context); } setValue(bean, assocBean); } } }
private void visitProperties(BeanDescriptor<?> desc, BeanPropertyVisitor propertyVisitor) { BeanProperty idProp = desc.getIdProperty(); if (idProp != null) { visit(propertyVisitor, idProp); } BeanPropertyAssocOne<?> unidirectional = desc.getUnidirectional(); if (unidirectional != null) { visit(propertyVisitor, unidirectional); } BeanProperty[] propertiesNonTransient = desc.propertiesNonTransient(); for (BeanProperty p : propertiesNonTransient) { if (p.isDDLColumn()) { visit(propertyVisitor, p); } } visitInheritanceProperties(desc, propertyVisitor); propertyVisitor.visitEnd(); }
@Override public void registerColumn(BeanDescriptor<?> desc, String prefix) { if (embedded) { for (BeanProperty prop : embeddedProps) { prop.registerColumn(desc, SplitName.add(prefix, name)); } } else { if (targetIdProperty != null) { BeanDescriptor<T> target = getTargetDescriptor(); String basePath = SplitName.add(prefix, name); if (dbColumn != null) { BeanProperty idProperty = target.getIdProperty(); desc.registerColumn(dbColumn, SplitName.add(basePath, idProperty.getName())); } desc.registerTable(target.getBaseTable(), this); } } }
/** * Register invalidation paths for embedded documents. */ @Override public void registerPaths() { if (mapped && !registerPaths) { Collection<PathProperties.Props> pathProps = docStructure.doc().getPathProps(); for (PathProperties.Props pathProp : pathProps) { String path = pathProp.getPath(); if (path != null) { BeanDescriptor<?> targetDesc = desc.getBeanDescriptor(path); BeanProperty idProperty = targetDesc.getIdProperty(); if (idProperty != null) { // embedded beans don't have id property String fullPath = path + "." + idProperty.getName(); targetDesc.docStoreAdapter().registerInvalidationPath(desc.getDocStoreQueueId(), fullPath, pathProp.getProperties()); } } } registerPaths = true; } }
/** * Set Id Generated value for insert. */ private void setIdGenValue(PersistRequestBean<?> request) { BeanDescriptor<?> desc = request.getBeanDescriptor(); if (!desc.isUseIdGenerator()) { return; } BeanProperty idProp = desc.getIdProperty(); if (idProp == null || idProp.isEmbedded()) { // not supporting IdGeneration for concatenated or Embedded return; } EntityBean bean = request.getEntityBean(); Object uid = idProp.getValue(bean); if (DmlUtil.isNullOrZero(uid)) { // generate the nextId and set it to the property Object nextId = desc.nextId(request.getTransaction()); // cast the data type if required and set it desc.convertSetId(nextId, bean); } }
public static void load(BeanDescriptor<?> desc, EntityBean bean, CachedBeanData cacheBeanData, PersistenceContext context) { EntityBeanIntercept ebi = bean._ebean_getIntercept(); BeanProperty idProperty = desc.getIdProperty(); if (desc.getInheritInfo() != null) { desc = desc.getInheritInfo().readType(bean.getClass()).desc(); } if (idProperty != null) { // load the id property loadProperty(bean, cacheBeanData, ebi, idProperty, context); } // load the non-many properties for (BeanProperty prop : desc.propertiesNonMany()) { loadProperty(bean, cacheBeanData, ebi, prop, context); } for (BeanPropertyAssocMany<?> prop : desc.propertiesMany()) { if (prop.isElementCollection()) { loadProperty(bean, cacheBeanData, ebi, prop, context); } else { prop.createReferenceIfNull(bean); } } ebi.setLoadedLazy(); }
/** * 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); }