/** * Returns the {@link SingularAttribute} representing the identifier of the given {@link EntityType} if it contains a * singular one. * * @param entityType must not be {@literal null}. * @return */ private static Optional<? extends SingularAttribute<?, ?>> getSingularIdAttribute(EntityType<?> entityType) { if (!entityType.hasSingleIdAttribute()) { return Optional.empty(); } return entityType.getSingularAttributes().stream() // .filter(SingularAttribute::isId) // .findFirst(); } }
/** * Creates a Map containing all properties (and their values) for a given * relationship entity * * @param entityMetadata * @param targetEntityMetadata * @param relationshipProperties * @param relationshipEntity */ public Map<String, Object> createRelationshipProperties(EntityMetadata entityMetadata, EntityMetadata targetEntityMetadata, Object relationshipEntity) { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( entityMetadata.getPersistenceUnit()); EntityType entityType = metaModel.entity(relationshipEntity.getClass()); Map<String, Object> relationshipProperties = new HashMap<String, Object>(); Set<Attribute> attributes = entityType.getSingularAttributes(); for (Attribute attribute : attributes) { Field f = (Field) attribute.getJavaMember(); if (!f.getType().equals(entityMetadata.getEntityClazz()) && !f.getType().equals(targetEntityMetadata.getEntityClazz())) { String relPropertyName = ((AbstractAttribute) attribute).getJPAColumnName(); Object value = PropertyAccessorHelper.getObject(relationshipEntity, f); relationshipProperties.put(relPropertyName, toNeo4JProperty(value)); } } return relationshipProperties; }
Set<Attribute> attributes = entityType.getSingularAttributes();
for (Attribute attribute : metaModel.entity(entityMetadata.getEntityClazz()).getSingularAttributes())
for (Attribute attribute : metaModel.entity(entityMetadata.getEntityClazz()).getSingularAttributes())
Set<Attribute> attributes = entityType.getSingularAttributes(); for (Attribute attribute : attributes)
EntityType entityType = metaModel.entity(relationshipObj.getClass()); Set<Attribute> attributes = entityType.getSingularAttributes(); for (Attribute attribute : attributes)
Set<Attribute> attributes = entityType.getSingularAttributes();
/** * Populates Node properties from Entity object * * @param entity * @param m * @param node */ private void populateNodeProperties(Object entity, EntityMetadata m, Node node) { MetamodelImpl metaModel = (MetamodelImpl) kunderaMetadata.getApplicationMetadata().getMetamodel( m.getPersistenceUnit()); EntityType entityType = metaModel.entity(m.getEntityClazz()); // Iterate over entity attributes Set<Attribute> attributes = entityType.getSingularAttributes(); for (Attribute attribute : attributes) { Field field = (Field) attribute.getJavaMember(); // Set Node level properties if (!attribute.isCollection() && !attribute.isAssociation() && !((SingularAttribute) attribute).isId()) { String columnName = ((AbstractAttribute) attribute).getJPAColumnName(); Object value = PropertyAccessorHelper.getObject(entity, field); if (value != null) { node.setProperty(columnName, toNeo4JProperty(value)); } } } }
@SuppressWarnings({"rawtypes", "unchecked"}) private SingularAttribute<?,?> getIdProperty(EntityType entity) { final Set<SingularAttribute> singularAttributes = entity.getSingularAttributes(); for (final SingularAttribute singularAttribute : singularAttributes) { if (singularAttribute.isId()) { return singularAttribute; } } return null; }
protected SingularAttribute<? super E, ?> getVersionAttribute(EntityType<E> entityType) { for (SingularAttribute<? super E, ?> sa : entityType.getSingularAttributes()) { if (sa.isVersion()) { return sa; } } return null; }
/** * _HACK_ too bad that JPA does not provide this entityType.getVersion(); * * @see http://stackoverflow.com/questions/13265094/generic-way-to-get-jpa-entity-version */ protected SingularAttribute<? super E, ?> getVersionAttribute(EntityType<E> entityType) { for (SingularAttribute<? super E, ?> sa : entityType.getSingularAttributes()) { if (sa.isVersion()) { return sa; } } return null; }
Set<Attribute> attributes = entityType.getSingularAttributes();
/** * Returns the {@link SingularAttribute} representing the identifier of the given {@link EntityType} if it contains a * singular one. * * @param entityType must not be {@literal null}. * @return */ private static Optional<? extends SingularAttribute<?, ?>> getSingularIdAttribute(EntityType<?> entityType) { if (!entityType.hasSingleIdAttribute()) { return Optional.empty(); } return entityType.getSingularAttributes().stream() // .filter(SingularAttribute::isId) // .findFirst(); } }
public static <X> SingularAttribute<? super X, ?> getIdAttribute(EntityType<X> et) { return Enumerable.create(et.getSingularAttributes()).firstOrNull(new Predicate1<SingularAttribute<? super X, ?>>() { public boolean apply(SingularAttribute<? super X, ?> input) { return input.isId(); } }); }
@SuppressWarnings("unchecked") private static String getEntityIdField(EntityManager em, Class entityClass) { String idProperty = ""; Metamodel metamodel = em.getMetamodel(); EntityType e = metamodel.entity(entityClass); Set<SingularAttribute> singularAttributes = e.getSingularAttributes(); for (SingularAttribute singularAttribute : singularAttributes) { if (singularAttribute.isId()) { idProperty = singularAttribute.getName(); break; } } return idProperty; }
private E fetchSingularAttributes(E entity, java.util.function.Predicate<Class<?>> ofType) { E managed = getById(entity.getId()); for (Attribute<?, ?> a : getMetamodel().getSingularAttributes()) { if (ofType.test(a.getJavaType())) { String name = capitalize(a.getName()); invokeSetter(entity, name, invokeGetter(managed, name)); } } return entity; }
@SuppressWarnings("unchecked") public <T> Predicate byPattern(Root<T> root, CriteriaBuilder builder, SearchParameters sp, Class<T> type) { if (!sp.hasSearchPattern()) { return null; } List<Predicate> predicates = newArrayList(); EntityType<T> entity = em.getMetamodel().entity(type); String pattern = sp.getSearchPattern(); for (SingularAttribute<? super T, ?> attr : entity.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) { continue; } if (attr.getJavaType() == String.class) { predicates.add(jpaUtil.stringPredicate((Expression<String>) root.get(jpaUtil.attribute(entity, attr)), pattern, sp, builder)); } } return jpaUtil.orPredicate(builder, predicates); } }
/** * Lookup entities having at least one String attribute matching the passed sp's pattern */ @SuppressWarnings("unchecked") public <T> Predicate byPattern(Root<T> root, CriteriaBuilder builder, SearchParameters sp, Class<T> type) { if (!sp.hasSearchPattern()) { return null; } List<Predicate> predicates = newArrayList(); EntityType<T> entity = em.getMetamodel().entity(type); String pattern = sp.getSearchPattern(); for (SingularAttribute<? super T, ?> attr : entity.getSingularAttributes()) { if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) { continue; } if (attr.getJavaType() == String.class) { predicates.add(jpaUtil.stringPredicate((Expression<String>) root.get(jpaUtil.attribute(entity, attr)), pattern, sp, builder)); } } return jpaUtil.orPredicate(builder, predicates); } }
private void analyze(T aObject) { if (aObject != null) { entityClass = HibernateProxyHelper.getClassWithoutInitializingProxy(aObject); String idProperty = null; Metamodel metamodel = getEntityManager().getMetamodel(); EntityType entity = metamodel.entity(entityClass); Set<SingularAttribute> singularAttributes = entity.getSingularAttributes(); for (SingularAttribute singularAttribute : singularAttributes) { if (singularAttribute.isId()) { idProperty = singularAttribute.getName(); break; } } if (idProperty == null) { throw new RuntimeException("id field not found"); } DirectFieldAccessor accessor = new DirectFieldAccessor(aObject); id = (Number) accessor.getPropertyValue(idProperty); } else { entityClass = null; id = null; } }