@Override public Iterable<String> getIdAttributeNames() { List<String> attributeNames = new ArrayList<String>(idMetadata.attributes.size()); for (SingularAttribute<? super T, ?> attribute : idMetadata.attributes) { attributeNames.add(attribute.getName()); } return attributeNames; }
@Override public boolean isNew(T entity) { if (!versionAttribute.isPresent() || versionAttribute.map(Attribute::getJavaType).map(Class::isPrimitive).orElse(false)) { return super.isNew(entity); } BeanWrapper wrapper = new DirectFieldAccessFallbackBeanWrapper(entity); return versionAttribute.map(it -> wrapper.getPropertyValue(it.getName()) == null).orElse(true); }
/** * Returns whether the attribute of given name and type is the single identifier attribute of the given entity. * * @param entity must not be {@literal null}. * @param name must not be {@literal null}. * @param attributeType must not be {@literal null}. * @return */ public boolean isSingleIdAttribute(Class<?> entity, String name, Class<?> attributeType) { return metamodel.getEntities().stream() // .filter(it -> entity.equals(it.getJavaType())) // .findFirst() // .flatMap(it -> getSingularIdAttribute(it)) // .filter(it -> it.getJavaType().equals(attributeType)) // .map(it -> it.getName().equals(name)) // .orElse(false); }
private String tryFindSingularIdAttributeNameOrUseFallback(Class<? extends Object> idPropertyValueType, String fallbackIdTypePropertyName) { ManagedType<? extends Object> idPropertyType = metamodel.managedType(idPropertyValueType); for (SingularAttribute<?, ?> sa : idPropertyType.getSingularAttributes()) { if (sa.isId()) { return sa.getName(); } } return fallbackIdTypePropertyName; }
@Override @Nullable @SuppressWarnings("unchecked") public ID getId(T entity) { BeanWrapper entityWrapper = new DirectFieldAccessFallbackBeanWrapper(entity); if (idMetadata.hasSimpleId()) { return (ID) entityWrapper.getPropertyValue(idMetadata.getSimpleIdAttribute().getName()); } BeanWrapper idWrapper = new IdentifierDerivingDirectFieldAccessFallbackBeanWrapper(idMetadata.getType(), metamodel); boolean partialIdValueFound = false; for (SingularAttribute<? super T, ?> attribute : idMetadata) { Object propertyValue = entityWrapper.getPropertyValue(attribute.getName()); if (propertyValue != null) { partialIdValueFound = true; } idWrapper.setPropertyValue(attribute.getName(), propertyValue); } return partialIdValueFound ? (ID) idWrapper.getWrappedInstance() : null; }
@Override @SuppressWarnings({ "unchecked" }) public <Y> Path<Y> get(SingularAttribute<? super X, Y> attribute) { if ( ! canBeDereferenced() ) { throw illegalDereference(); } SingularAttributePath<Y> path = (SingularAttributePath<Y>) resolveCachedAttributePath( attribute.getName() ); if ( path == null ) { path = new SingularAttributePath<Y>( criteriaBuilder(), attribute.getJavaType(), getPathSourceForSubPaths(), attribute ); registerAttributePath( attribute.getName(), path ); } return path; }
String currentPath = !StringUtils.hasText(path) ? attribute.getName() : path + "." + attribute.getName(); .apply(Optional.ofNullable(beanWrapper.getPropertyValue(attribute.getName()))); predicates.addAll(getPredicates(currentPath, cb, from.get(attribute.getName()), (ManagedType<?>) attribute.getType(), attributeValue, probeType, exampleAccessor, currentNode)); continue; PathNode node = currentNode.add(attribute.getName(), attributeValue); if (node.spansCycle()) { throw new InvalidDataAccessApiUsageException( predicates.addAll(getPredicates(currentPath, cb, ((From<?, ?>) from).join(attribute.getName()), (ManagedType<?>) attribute.getType(), attributeValue, probeType, exampleAccessor, node));
role.insert( 1, singularAttributePath.getAttribute().getName() ); parentPath = singularAttributePath.getPathSource(); } while ( ( SingularAttributePath.class.isInstance( parentPath ) ) );
String original = ((SingularAttributePath) path).getAttribute().getDeclaringType().getJavaType().getName() + "." + ((SingularAttributePath) path).getAttribute().getName() + "." + piece; String copy = path.getJavaType().getName() + "." + piece; copyCollectionPersister(original, copy, ((CriteriaBuilderImpl) builder).getEntityManagerFactory().getSessionFactory());
.getPropertyValue(nestedEntityInformation.getRequiredIdAttribute().getName()); super.setPropertyValue(propertyName, nestedIdPropertyValue); return;
private void fillConditions(Query q) { for (List<Cond> conds : andConditions.values()) { for (int i=0; i<conds.size(); i++) { Cond cond = conds.get(i); if (Op.NULL == cond.op || Op.NOT_NULL == cond.op) { continue; } q.setParameter("cond_" + cond.attr.getName() + String.format("_%s", i), cond.val); } } }
@Override public UpdateQuery set(SingularAttribute attr, Object val) { if (setValues.containsKey(attr)) { throw new CloudRuntimeException(String.format("unable to set a column[%s] twice", attr.getName())); } setValues.put(attr, val); return this; }
/** * @param entityClassName * @param em * @return */ public static String buildOutput(Class<?> entityClass, EntityMetadata entityMetadata, Object output) { StringBuilder sb = new StringBuilder("'"); sb.append("{\"").append(entityClass.getSimpleName().toLowerCase()).append("\":").append(output) .append(",\"entityClassName\":\"").append(entityMetadata.getEntityClazz().getSimpleName()) .append("\",\"id\":\"" + entityMetadata.getIdAttribute().getName()).append("\"}").append("'"); return sb.toString(); }
private String where() { if (andConditions.isEmpty()) { return null; } List<String> condstrs = new ArrayList<>(); for (List<Cond> conds : andConditions.values()) { for (int i=0; i<conds.size(); i++) { Cond cond = conds.get(i); String condName = String.format("cond_%s_%s", cond.attr.getName(), i); if (Op.IN == cond.op || Op.NOT_IN == cond.op) { condstrs.add(String.format("vo.%s %s (:%s)", cond.attr.getName(), cond.op.toString(), condName)); } else if (Op.NULL == cond.op || Op.NOT_NULL == cond.op) { condstrs.add(String.format("vo.%s %s", cond.attr.getName(), cond.op)); } else { condstrs.add(String.format("vo.%s %s :%s", cond.attr.getName(), cond.op.toString(), condName)); } } } return StringUtils.join(condstrs, " AND "); }
public String getFieldName(String jpaColumnName) { // if(jpaColumnName.equals(((AbstractAttribute)this.getIdAttribute()).getJPAColumnName())) // { // return this.getIdAttribute().getName(); // } String fieldName = jpaColumnMapping.get(jpaColumnName); if (fieldName == null) { getEntityType(); MetadataUtils.onJPAColumnMapping(this.entityType, this); // rebase. // require // in case // of // concrete // super // entity // class. fieldName = jpaColumnMapping.get(jpaColumnName); } if (fieldName == null && jpaColumnName.equals(((AbstractAttribute) this.getIdAttribute()).getJPAColumnName())) { return this.getIdAttribute().getName(); } return fieldName; }
/** * Method to add specific relation name for given relational field. * * @param rField * relation object. */ private void addRelationName(Relation rField) { if (rField != null && !rField.isRelatedViaJoinTable()) { String relationName = getJoinColumnName(rField.getProperty()); if (rField.getProperty().isAnnotationPresent(PrimaryKeyJoinColumn.class)) { relationName = this.getIdAttribute().getName(); } addToRelationNameCollection(relationName); } }
/** * Gets (if available) or creates a relationship for the given entity */ private Relationship getOrCreateRelationshipWithUniqueFactory(Object entity, EntityMetadata m, GraphDatabaseService graphDb) { Object id = PropertyAccessorHelper.getObject(entity, (Field) m.getIdAttribute().getJavaMember()); final String idFieldName = m.getIdAttribute().getName(); UniqueFactory<Relationship> factory = new UniqueFactory.UniqueRelationshipFactory(graphDb, m.getIndexName()) { @Override protected Relationship create(Map<String, Object> paramMap) { return null; } @Override protected void initialize(Relationship relationship, Map<String, Object> properties) { relationship.setProperty(idFieldName, properties.get(idFieldName)); } }; return factory.getOrCreate(idFieldName, id); }
private void rollbackJoinTableData(PersistenceDelegator delegator) { // on deleting join table data. for (JoinTableData jtData : joinTableDataCollection) { if (jtData.isProcessed()) { EntityMetadata m = KunderaMetadataManager.getEntityMetadata(delegator.getKunderaMetadata(), jtData.getEntityClass()); Client client = delegator.getClient(m); if (OPERATION.INSERT.equals(jtData.getOperation())) { for (Object pk : jtData.getJoinTableRecords().keySet()) { client.deleteByColumn(jtData.getSchemaName(), jtData.getJoinTableName(), m.getIdAttribute() .getName(), pk); } } else if (OPERATION.DELETE.equals(jtData.getOperation())) { client.persistJoinTable(jtData); } } } joinTableDataCollection.clear(); joinTableDataCollection = null; joinTableDataCollection = new ArrayList<JoinTableData>(); }
@Test public void testMappedMetamodel() { EntityMetadata entityMetadata = KunderaMetadataManager.getEntityMetadata( ((EntityManagerFactoryImpl) emf).getKunderaMetadataInstance(), Employee.class); MetamodelImpl metaModel = (MetamodelImpl) ((EntityManagerFactoryImpl) emf).getKunderaMetadataInstance() .getApplicationMetadata().getMetamodel(persistenceUnit); EntityType entityType = metaModel.entity(Employee.class); Set<Attribute> attributes = entityType.getAttributes(); Assert.assertEquals(5, attributes.size()); Assert.assertNotNull(entityMetadata.getIdAttribute()); Assert.assertNotNull("id", entityMetadata.getIdAttribute().getName()); }