List of usage examples for javax.persistence.metamodel Attribute getName
String getName();
From source file:org.querybyexample.jpa.JpaUtil.java
public static <T, A> SingularAttribute<? super T, A> attribute(ManagedType<? super T> mt, Attribute<? super T, A> attr) { return mt.getSingularAttribute(attr.getName(), attr.getJavaType()); }
From source file:org.querybyexample.jpa.JpaUtil.java
public static <T> SingularAttribute<? super T, String> stringAttribute(ManagedType<? super T> mt, Attribute<? super T, ?> attr) { return mt.getSingularAttribute(attr.getName(), String.class); }
From source file:org.querybyexample.jpa.JpaUtil.java
public static String getPath(List<Attribute<?, ?>> attributes) { StringBuilder builder = new StringBuilder(); for (Attribute<?, ?> attribute : attributes) { builder.append(attribute.getName()).append("."); }/* w w w. j a v a 2 s . com*/ return builder.substring(0, builder.length() - 1); }
From source file:org.querybyexample.jpa.JpaUtil.java
/** * Convert the passed propertyPath into a JPA path. * <p>/*www . ja v a2 s .com*/ * Note: JPA will do joins if the property is in an associated entity. */ @SuppressWarnings("unchecked") public static <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) { Path<?> path = root; for (Attribute<?, ?> attribute : attributes) { boolean found = false; // handle case when order on already fetched attribute for (Fetch<E, ?> fetch : root.getFetches()) { if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) { path = (Join<E, ?>) fetch; found = true; break; } } for (Join<E, ?> join : root.getJoins()) { if (attribute.getName().equals(join.getAttribute().getName())) { path = join; found = true; break; } } if (!found) { path = path.get(attribute.getName()); } } return (Path<F>) path; }
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Initialize a entity. /*from www .j a v a 2 s .c om*/ * @param em entity manager to use * @param entity entity to initialize * @param depth max depth on recursion */ @SuppressWarnings({ "rawtypes", "unchecked" }) public static void initialize(EntityManager em, Object entity, int depth) { // return on nulls, depth = 0 or already initialized objects if (entity == null || depth == 0) { return; } PersistenceUnitUtil unitUtil = em.getEntityManagerFactory().getPersistenceUnitUtil(); EntityType entityType = em.getMetamodel().entity(entity.getClass()); Set<Attribute> attributes = entityType.getDeclaredAttributes(); Object id = unitUtil.getIdentifier(entity); if (id != null) { Object attached = em.find(entity.getClass(), unitUtil.getIdentifier(entity)); for (Attribute a : attributes) { if (!unitUtil.isLoaded(entity, a.getName())) { if (a.isCollection()) { intializeCollection(em, entity, attached, a, depth); } else if (a.isAssociation()) { intialize(em, entity, attached, a, depth); } } } } }
From source file:com.github.gekoh.yagen.util.MappingUtils.java
private static void appendNonCollectionReferences(Set<Attribute> attributes, Map<String, Field> fields, String context) {/*from www. ja v a 2 s . com*/ for (Attribute attribute : attributes) { String attributeName = context + attribute.getName(); if (attribute instanceof SingularAttribute && ((SingularAttribute) attribute).getType() instanceof EmbeddableType) { appendNonCollectionReferences( ((EmbeddableType) ((SingularAttribute) attribute).getType()).getAttributes(), fields, attributeName + "."); } else if (!attribute.isCollection() && attribute.getPersistentAttributeType() != Attribute.PersistentAttributeType.BASIC && attribute.getDeclaringType() instanceof EntityType) { fields.put(attributeName, (Field) attribute.getJavaMember()); } } }
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Initialize entity attribute//from www .ja v a2s . c o m * @param em * @param entity * @param a * @param depth */ @SuppressWarnings("rawtypes") private static void intialize(EntityManager em, Object entity, Object attached, Attribute a, int depth) { Object value = PropertyAccessorFactory.forDirectFieldAccess(attached).getPropertyValue(a.getName()); if (!em.getEntityManagerFactory().getPersistenceUnitUtil().isLoaded(value)) { em.refresh(value); } PropertyAccessorFactory.forDirectFieldAccess(entity).setPropertyValue(a.getName(), value); initialize(em, value, depth - 1); }
From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java
/** * To entity meta data.//from w w w.ja v a 2s . c o m * * @param mt the mt * @return the entity metadata */ private static EntityMetadata toEntityMetaData(ManagedType<?> mt) { EntityMetadataBean ret = new EntityMetadataBean(); ret.setJavaType(mt.getJavaType()); if (mt instanceof EntityType) { EntityType ift = (EntityType) mt; ret.setDatabaseName(ift.getName()); } // TODO RK delete propDesc List<PropertyDescriptor> propDescs = Arrays.asList(PropertyUtils.getPropertyDescriptors(ret.getJavaType())); Set<?> attr = mt.getAttributes(); for (Object oa : attr) { Attribute<?, ?> at = (Attribute<?, ?>) oa; Optional<PropertyDescriptor> pdo = propDescs.stream().filter((el) -> el.getName().equals(at.getName())) .findFirst(); ColumnMetadata colm = getColumnMetaData(ret, mt.getJavaType(), at, pdo); if (colm != null) { ret.getColumns().put(colm.getName(), colm); } } /// EntityType.getName() is not correct. Table[] tabs = mt.getJavaType().getAnnotationsByType(Table.class); if (tabs != null && tabs.length > 0) { for (Table tab : tabs) { if (StringUtils.isNotBlank(tab.name()) == true) { ret.setDatabaseName(tab.name()); break; } } } return ret; }
From source file:de.micromata.genome.jpa.metainf.MetaInfoUtils.java
/** * Gets the column meta data./* w ww . j av a 2 s.com*/ * * @param entity the entity * @param entityClass the entity class * @param at the at * @param pdo the pdo * @return the column meta data */ public static ColumnMetadata getColumnMetaData(EntityMetadataBean entity, Class<?> entityClass, Attribute<?, ?> at, Optional<PropertyDescriptor> pdo) { ColumnMetadataBean ret = new ColumnMetadataBean(entity); ret.setName(at.getName()); ret.setJavaType(at.getJavaType()); ret.setAssociation(at.isAssociation()); ret.setCollection(at.isCollection()); if (at instanceof PluralAttribute) { PluralAttribute pa = (PluralAttribute) at; Type eltype = pa.getElementType(); CollectionType coltype = pa.getCollectionType(); } Member jm = at.getJavaMember(); // TODO maybe handle this. at.getPersistentAttributeType(); if ((jm instanceof AccessibleObject) == false) { LOG.warn("Column " + at.getName() + " ha no valid Java Member"); return ret; } AccessibleObject ao = (AccessibleObject) jm; getGetterSetter(entityClass, ao, pdo, ret); ret.setAnnotations(getFieldAndMemberAnnots(entityClass, ao)); if (ret.getAnnotations().stream().filter((anot) -> anot.getClass() == Transient.class).count() > 0) { return null; } Column colc = ao.getAnnotation(Column.class); if (colc == null) { ret.setMaxLength(255); return ret; } String name = colc.name(); if (StringUtils.isEmpty(name) == true) { ret.setDatabaseName(ret.getName()); } else { ret.setDatabaseName(name); } ret.setMaxLength(colc.length()); ret.setUnique(colc.unique()); ret.setColumnDefinition(colc.columnDefinition()); ret.setInsertable(colc.insertable()); ret.setNullable(colc.nullable()); ret.setPrecision(colc.precision()); ret.setScale(colc.scale()); return ret; }
From source file:org.jdal.dao.jpa.JpaUtils.java
/** * Initialize collection/* w ww.ja v a2 s. com*/ * @param em * @param entity * @param a * @param i */ @SuppressWarnings("rawtypes") private static void intializeCollection(EntityManager em, Object entity, Object attached, Attribute a, int depth) { PropertyAccessor accessor = PropertyAccessorFactory.forDirectFieldAccess(attached); Collection c = (Collection) accessor.getPropertyValue(a.getName()); for (Object o : c) initialize(em, o, depth - 1); PropertyAccessorFactory.forDirectFieldAccess(entity).setPropertyValue(a.getName(), c); }