private List<Map<String, ?>> recordsInfo() { List<Map<String, ?>> records = U.list(); for (EntityType<?> type : JPA.getEntityTypes()) { Class<?> javaType = type.getJavaType(); long count = JPA.count(javaType); String idType = type.getIdType() != null ? type.getIdType().getJavaType().getSimpleName() : ""; Object superType = type.getSupertype() != null ? type.getSupertype().getJavaType().getSimpleName() : ""; records.add(U.map("type", type.getName(), "extends", superType, "ID Type", idType, "count", count)); } return records; }
SingularAttribute<?, ?> id = root.getModel().getId(root.getModel().getIdType().getJavaType()); query = query.multiselect(root.get((SingularAttribute) id).alias(id.getName()));
private SingularAttribute<?, ?> getIdentifierAttribute(final Class<?> entityClass) { if (!idAttibuteCache.containsKey(entityClass)) { final EntityType<?> type = getEntityType(entityClass); // patch for openjpa because it returns null instead of IllegalArgumentException // when class is not an entity. Assert.notNull(type, "Not an entity: " + entityClass); idAttibuteCache.put(entityClass, type.getId(type.getIdType().getJavaType())); } return idAttibuteCache.get(entityClass); }
protected boolean isValidKeyType(Object key) { return emf.getMetamodel().entity(configuration.entityClass()).getIdType().getJavaType().isAssignableFrom(key.getClass()); }
@Override protected void renderArguments( StringBuilder buffer, CriteriaQueryCompiler.RenderingContext renderingContext ) { if (isDistinct()) buffer.append("distinct "); else { // If function specifies a single non-distinct entity with ID, its alias would normally be rendered, which ends up // converting to the column(s) associated with the entity's ID in the rendered SQL. However, some DBs don't support // the multiple columns that would end up here for entities with composite IDs. So, since we modify the query to // instead specify star since that's functionally equivalent and supported by all DBs. List<Expression<?>> argExprs = getArgumentExpressions(); if (argExprs.size() == 1) { Expression argExpr = argExprs.get(0); if (argExpr instanceof Root<?>) { Root<?> root = (Root<?>)argExpr; if (root.getModel().getIdType() != null) { buffer.append('*'); return; } } } } super.renderArguments(buffer, renderingContext); }
private List<Map<String, ?>> recordsInfo() { List<Map<String, ?>> records = U.list(); for (EntityType<?> type : JPA.getEntityTypes()) { Class<?> javaType = type.getJavaType(); long count = JPA.count(javaType); String idType = type.getIdType() != null ? type.getIdType().getJavaType().getSimpleName() : ""; Object superType = type.getSupertype() != null ? type.getSupertype().getJavaType().getSimpleName() : ""; records.add(U.map("type", type.getName(), "extends", superType, "ID Type", idType, "count", count)); } return records; }
protected <X>Attribute<? super X, ?> idAttribute(final Class<X> klazz) { final EntityType<X> type = em.getMetamodel().entity(klazz); final Class<?> idType = type.getIdType().getJavaType(); final Attribute<? super X, ?> attribute = type.getId(idType); return attribute; }
@Override public List<String> getIdentifierOrUniqueKeyEmbeddedPropertyNames(EntityType<?> ownerType, String attributeName) { Attribute<?, ?> attribute = getAttribute(ownerType, attributeName); if (((SingularAttribute<?, ?>) attribute).getType() instanceof EntityType<?>) { EntityType<?> entityType = (EntityType<?>) ((SingularAttribute<?, ?>) attribute).getType(); if (entityType.hasSingleIdAttribute()) { return Collections.singletonList(entityType.getId(entityType.getIdType().getJavaType()).getName()); } else { Set<SingularAttribute<?, ?>> attributes = (Set<SingularAttribute<?, ?>>) (Set) entityType.getIdClassAttributes(); List<String> attributeNames = new ArrayList<>(attributes.size()); for (Attribute<?, ?> attr : attributes) { attributeNames.add(attr.getName()); } return attributeNames; } } return Collections.emptyList(); }
/** * Sets the ID of the given object, which must be a JPA entity, to its default * value. The default value for reference types is {@code null}; the default * value for primitive types is the same as the JVM default value for an * uninitialized field. * * @param entity * the JPA entity whose ID value to clear */ private <X> void clearId(X entity) { // XXX probably need to pass in the actual entity class rather than this cast // (because dynamic proxies will fool it) @SuppressWarnings("unchecked") EntityType<X> type = em.getMetamodel().entity((Class<X>) entity.getClass()); SingularAttribute<? super X, ?> attr = type.getId(type.getIdType().getJavaType()); attributeAccessor.set(attr, entity, null); }
static Object typeSafeEntityKey( EntityManager em, EntityType<?> jpaEntityType, OEntityKey entityKey) { if (entityKey != null && jpaEntityType.getIdType().getPersistenceType() == PersistenceType.EMBEDDABLE) { Object id = newInstance(jpaEntityType.getIdType().getJavaType()); applyOProperties( em, em.getMetamodel().embeddable( jpaEntityType.getIdType().getJavaType()), entityKey.asComplexProperties(), id); return id; } Class<?> javaType = jpaEntityType.getIdType().getJavaType(); try { return TypeConverter.convert(entityKey == null ? null : entityKey.asSingleValue(), javaType); } catch (UnsupportedOperationException e) { throw new BadRequestException("Invalid key type", e); } catch (IllegalArgumentException e) { throw new BadRequestException("Invalid key value", e); } }
/** * Returns the ID of the given object, which must be a JPA entity. * * @param entity * the JPA entity whose ID value to retrieve * @return The ID of the given entity. If the entity ID type is primitive (for * example, {@code int} as opposed to {@code Integer}), the * corresponding boxed value will be returned. */ private <X> Object id(X entity) { // XXX probably need to pass in the actual entity class rather than this cast // (because dynamic proxies will fool it) @SuppressWarnings("unchecked") EntityType<X> type = em.getMetamodel().entity((Class<X>) entity.getClass()); SingularAttribute<? super X, ?> attr = type.getId(type.getIdType().getJavaType()); return attributeAccessor.get(attr, entity); }
@Override public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor targetType) { EntityType<?> entityType = em.getMetamodel().entity(targetType.getType()); Class<?> idType = entityType.getIdType().getJavaType(); Object id = identifierConverter.convert(source, sourceType, TypeDescriptor.valueOf(idType)); return em.find(targetType.getType(), id); } }
private String getIdAttribute(Class<?> entityClass) { EntityType<?> entityType = entityManager.getMetamodel().entity(entityClass); return entityType.getDeclaredId(entityType.getIdType().getJavaType()).getName(); }
public JpaValueEncoder(final EntityType<E> entity, final EntityManagerManager entityManagerManager, final String persistenceUnitName, final PropertyAccess propertyAccess, final TypeCoercer typeCoercer, final Logger logger) { super(); this.entity = entity; this.entityManagerManager = entityManagerManager; this.persistenceUnitName = persistenceUnitName; this.typeCoercer = typeCoercer; this.logger = logger; final Type<?> idType = this.entity.getIdType(); final SingularAttribute<? super E, ?> idAttribute = this.entity.getId(idType.getJavaType()); idPropertyName = idAttribute.getName(); propertyAdapter = propertyAccess.getAdapter(entity.getJavaType()).getPropertyAdapter( idPropertyName); }
public JpaValueEncoder(final EntityType<E> entity, final EntityManagerManager entityManagerManager, final String persistenceUnitName, final PropertyAccess propertyAccess, final TypeCoercer typeCoercer, final Logger logger) { super(); this.entity = entity; this.entityManagerManager = entityManagerManager; this.persistenceUnitName = persistenceUnitName; this.typeCoercer = typeCoercer; this.logger = logger; final Type<?> idType = this.entity.getIdType(); final SingularAttribute<? super E, ?> idAttribute = this.entity.getId(idType.getJavaType()); idPropertyName = idAttribute.getName(); propertyAdapter = propertyAccess.getAdapter(entity.getJavaType()).getPropertyAdapter( idPropertyName); }
static Object createNewJPAEntity( EntityManager em, EntityType<?> jpaEntityType, OEntity oEntity, boolean withLinks) { Object jpaEntity = newInstance(jpaEntityType.getJavaType()); if (jpaEntityType.getIdType().getPersistenceType() == PersistenceType.EMBEDDABLE) { EmbeddableType<?> et = (EmbeddableType<?>) jpaEntityType .getIdType(); JPAMember idMember = JPAMember.create( jpaEntityType.getId(et.getJavaType()), jpaEntity); Object idValue = newInstance(et.getJavaType()); idMember.set(idValue); } applyOProperties(em, jpaEntityType, oEntity.getProperties(), jpaEntity); if (withLinks) applyOLinks(em, jpaEntityType, oEntity.getLinks(), jpaEntity); return jpaEntity; }
/** * if query orders are empty, then apply default ascending ordering * by root id attribute to prevent paging inconsistencies * * @param query * @param from * @param cb */ protected void mayBeAddDefaultOrderBy(CriteriaQuery<?> query, From<?,?> from, CriteriaBuilder cb) { if (query.getOrderList() == null || query.getOrderList().isEmpty()) { EntityType<?> fromEntityType = entityType; try { java.lang.reflect.Field sortField = getSortAnnotation(fromEntityType.getBindableJavaType()); if (sortField == null) query.orderBy(cb.asc(from.get(fromEntityType.getId(fromEntityType.getIdType().getJavaType()).getName()))); else { GraphQLDefaultOrderBy order = sortField.getAnnotation(GraphQLDefaultOrderBy.class); if (order.asc()) { query.orderBy(cb.asc(from.get(sortField.getName()))); } else { query.orderBy(cb.desc(from.get(sortField.getName()))); } } } catch (Exception ex) { //log.warn("In" + this.getClass().getName(), ex); } } }
public static EntityTypeDescriptor createSimpleEntityTypeDescriptor(EntityManagerFactory emf, Class<?> clazz) { EntityType<?> entityType = emf.getMetamodel().entity( clazz ); SingularAttribute<?, ?> idAttribute = entityType.getId( entityType.getIdType().getJavaType() ); return new EntityTypeDescriptor( clazz, new SingularIdOrder( idAttribute.getName() ) ); }