本文整理汇总了Java中javax.persistence.metamodel.Attribute类的典型用法代码示例。如果您正苦于以下问题:Java Attribute类的具体用法?Java Attribute怎么用?Java Attribute使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Attribute类属于javax.persistence.metamodel包,在下文中一共展示了Attribute类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getJpaHeaders
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
/**
* Return JPA managed properties.
*
* @param <T>
* Bean type.
* @param beanType
* the bean type.
* @return the headers built from given type.
*/
public <T> String[] getJpaHeaders(final Class<T> beanType) {
// Build descriptor list respecting the declaration order
final OrderedFieldCallback fieldCallBack = new OrderedFieldCallback();
ReflectionUtils.doWithFields(beanType, fieldCallBack);
final List<String> orderedDescriptors = fieldCallBack.descriptorsOrdered;
// Now filter the properties
final List<String> descriptorsFiltered = new ArrayList<>();
final ManagedType<T> managedType = transactionManager.getEntityManagerFactory().getMetamodel().managedType(beanType);
for (final String propertyDescriptor : orderedDescriptors) {
for (final Attribute<?, ?> attribute : managedType.getAttributes()) {
// Match only basic attributes
if (attribute instanceof SingularAttribute<?, ?> && propertyDescriptor.equals(attribute.getName())) {
descriptorsFiltered.add(attribute.getName());
break;
}
}
}
// Initialize the CSV reader
return descriptorsFiltered.toArray(new String[descriptorsFiltered.size()]);
}
示例2: cleanup
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
/**
* Delete the managed entities. Self referencing rows are set to NULL before the deletion.
*
* @param beanTypes
* the ordered set to clean.
*/
public void cleanup(final Class<?>... beanTypes) {
// Clean the data
for (int i = beanTypes.length; i-- > 0;) {
final Class<?> entityClass = beanTypes[i];
final String update = em.getMetamodel().managedType(entityClass).getAttributes().stream().filter(a -> a.getJavaType().equals(entityClass))
.map(Attribute::getName).map(name -> name + "=NULL").collect(Collectors.joining(", "));
if (update.length() > 0) {
// Clear the self referencing rows
em.createQuery("UPDATE " + entityClass.getName() + " SET " + update).executeUpdate();
}
em.createQuery("DELETE FROM " + entityClass.getName()).executeUpdate();
}
em.flush();
}
示例3: byPattern
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
/**
* Lookup entities having at least one String attribute matching the passed sp's pattern
*/
@SuppressWarnings("unused")
public <T> Predicate byPattern(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder, final SearchParameters sp, final Class<T> type) {
if (!sp.hasSearchPattern()) {
return null;
}
List<Predicate> predicates = newArrayList();
EntityType<T> entity = em.getMetamodel().entity(type);
String pattern = sp.getSearchPattern();
for (Attribute<T, ?> attr : entity.getDeclaredSingularAttributes()) {
if (attr.getPersistentAttributeType() == MANY_TO_ONE || attr.getPersistentAttributeType() == ONE_TO_ONE) {
continue;
}
if (attr.getJavaType() == String.class) {
predicates.add(JpaUtil.stringPredicate(root.get(attribute(entity, attr)), pattern, sp, builder));
}
}
return JpaUtil.orPredicate(builder, predicates);
}
示例4: filter
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public Attribute<?, ?> filter() {
Type<?> type = forModel(metamodel).filter(rootType);
Attribute<?, ?> result = null;
for (int i = 1; i < pathElements.length; i++) {
if (!(type instanceof ManagedType)) {
throw new PersistenceException("Cannot navigate through simple property "
+ pathElements[i] + " of type " + type.getJavaType());
}
result = ((ManagedType<?>)type).getAttribute(pathElements[i]);
if (result.isCollection()) {
type = ((PluralAttribute<?, ?, ?>)result).getElementType();
} else {
type = ((SingularAttribute<?, ?>)result).getType();
}
}
return result;
}
示例5: visit
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public boolean visit(JpqlPath node, Set<TypeDefinition> typeDefinitions) {
Alias alias = new Alias(node.jjtGetChild(0).getValue());
Class<?> type = getType(alias, typeDefinitions);
for (int i = 1; i < node.jjtGetNumChildren(); i++) {
ManagedType<?> managedType = forModel(metamodel).filter(type);
String attributeName = node.jjtGetChild(i).getValue();
Attribute<?, ?> attribute = managedType.getAttribute(attributeName);
if (attribute instanceof SingularAttribute
&& ((SingularAttribute<?, ?>)attribute).getType().getPersistenceType() == PersistenceType.BASIC
&& i < node.jjtGetNumChildren() - 1) {
String error = "Cannot navigate through simple property "
+ attributeName + " in class " + type.getName();
throw new PersistenceException(error);
}
type = attribute.getJavaType();
}
return false;
}
示例6: visitJoin
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public boolean visitJoin(Node node, Set<TypeDefinition> typeDefinitions) {
if (node.jjtGetNumChildren() != 2) {
return false;
}
Node pathNode = node.jjtGetChild(0);
Node aliasNode = node.jjtGetChild(1);
Alias rootAlias = new Alias(pathNode.jjtGetChild(0).toString());
Class<?> rootType = getType(rootAlias, typeDefinitions);
ManagedType<?> managedType = forModel(metamodel).filter(rootType);
for (int i = 1; i < pathNode.jjtGetNumChildren(); i++) {
Attribute<?, ?> attribute = managedType.getAttribute(pathNode.jjtGetChild(i).toString());
if (attribute.getPersistentAttributeType() == PersistentAttributeType.BASIC) {
throw new PersistenceException("Cannot navigate through basic property "
+ pathNode.jjtGetChild(i) + " of path " + pathNode);
}
if (attribute.isCollection()) {
PluralAttribute<?, ?, ?> pluralAttribute = (PluralAttribute<?, ?, ?>)attribute;
managedType = (ManagedType<?>)pluralAttribute.getElementType();
} else {
managedType = (ManagedType<?>)((SingularAttribute)attribute).getType();
}
}
typeDefinitions.add(new TypeDefinition(new Alias(aliasNode.toString()), managedType.getJavaType()));
return false;
}
示例7: extractNativeSqlColumnName
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public static String extractNativeSqlColumnName(final Attribute<?, ?> attr) {
switch (attr.getPersistentAttributeType()) {
case BASIC:
case EMBEDDED:
return attr.getName();
case ELEMENT_COLLECTION:
case ONE_TO_ONE:
case MANY_TO_ONE:
return attr.getName() + ID_SUFFIX;
case MANY_TO_MANY:
case ONE_TO_MANY:
throw new IllegalArgumentException(PersistentAttributeType.class.getSimpleName() + " ["
+ attr.getPersistentAttributeType() + "] is not supported!");
default:
throw UnknownArgumentException.newInstance(PersistentAttributeType.class, attr.getPersistentAttributeType());
}
}
示例8: dropIndexNewTx
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
@Transactional
private void dropIndexNewTx(final Class<?> entityClass, final Index index, final EntityManager em,
final UniqueNameGenerator uniqueNameGenerator) {
Assertions.assertThat(index.columnNames().length).isGreaterThan(0);
final String comma = ", ";
final String name;
if (Strings.isNotBlank(index.name())) {
name = index.name();
} else {
name = "idx" + entityClass.getSimpleName();
}
final StringBuilder cols = new StringBuilder();
final ManagedType<?> managedType = em.getMetamodel().managedType(entityClass);
for (final String columnName : index.columnNames()) {
if (cols.length() > 0) {
cols.append(comma);
}
final Attribute<?, ?> column = Attributes.findAttribute(managedType, columnName);
cols.append(Attributes.extractNativeSqlColumnName(column));
}
final String create = "DROP INDEX " + uniqueNameGenerator.get(name) + " ON " + entityClass.getSimpleName();
em.createNativeQuery(create).executeUpdate();
}
示例9: copyProperties
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
/**
* Copies the persistent properties from the source to the destination entity
*/
public void copyProperties(final Entity source, final Entity dest) {
if (source == null || dest == null) {
return;
}
final ManagedType<?> metaData = getClassMetamodel(source);
for (Attribute<?, ?> attribute : metaData.getAttributes()) {
// Skip the collections
if (attribute.isCollection()) {
PropertyHelper.set(dest, attribute.getName(), null);
} else {
PropertyHelper.set(dest, attribute.getName(), PropertyHelper.get(source, attribute.getName()));
}
}
}
示例10: testMetaModel
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
/**
* Méthode permettant de s'assurer que les attributs des classes marquées @Entity ne seront pas sérialisés en
* "bytea" lors de leur écriture en base.
*
* @param classesAutorisees : concerne uniquement des classes matérialisées. Si une enum fait péter le test, c'est
* qu'il manque l'annotation @Enumerated ou que celle-ci prend EnumType.ORDINAL en paramètre
*/
protected void testMetaModel(List<Attribute<?, ?>> ignoredAttributes, Class<?>... classesAutorisees) throws NoSuchFieldException, SecurityException {
List<Class<?>> listeAutorisee = Lists.newArrayList();
listeAutorisee.add(String.class);
listeAutorisee.add(Long.class);
listeAutorisee.add(Double.class);
listeAutorisee.add(Integer.class);
listeAutorisee.add(Float.class);
listeAutorisee.add(Date.class);
listeAutorisee.add(BigDecimal.class);
listeAutorisee.add(Boolean.class);
listeAutorisee.add(int.class);
listeAutorisee.add(long.class);
listeAutorisee.add(double.class);
listeAutorisee.add(boolean.class);
listeAutorisee.add(float.class);
for (Class<?> clazz : classesAutorisees) {
listeAutorisee.add(clazz);
}
for (EntityType<?> entityType : getEntityManager().getMetamodel().getEntities()) {
for (Attribute<?, ?> attribute : entityType.getDeclaredAttributes()) {
testMetaModel(attribute, listeAutorisee, ignoredAttributes);
}
}
}
示例11: shouldReturnListOfFieldsForAtttributes
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
@Test
public void shouldReturnListOfFieldsForAtttributes() throws Exception {
EntityManagerProvider.init();
final List<Attribute<?, ?>> attributes = new ArrayList<Attribute<?, ?>>();
attributes.add(A_.id);
attributes.add(B_.id);
final List<Field> fields = AttributeHelper.getFields(attributes);
Assert.assertEquals(2, fields.size());
Assert.assertEquals("id", fields.get(0).getName());
Assert.assertEquals(A.class, fields.get(0).getDeclaringClass());
Assert.assertEquals("id", fields.get(1).getName());
Assert.assertEquals(B.class, fields.get(1).getDeclaringClass());
}
示例12: printPersistenceModel
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public void printPersistenceModel() {
Metamodel metaModel = entityManager.getMetamodel();
Set<EntityType<? extends Object>> types = metaModel.getEntities();
for(EntityType<? extends Object> type : types) {
logger.log(Level.INFO, "--> Type: {0}", type);
Set attributes = type.getAttributes();
for(Object obj : attributes) {
logger.log(Level.INFO, "Name: {0}", ((Attribute)obj).getName());
logger.log(Level.INFO, "isCollection: {0}", ((Attribute)obj).isCollection());
logger.log(Level.INFO, "Name: {0}", ((Attribute)obj).isAssociation());
logger.log(Level.INFO, "Name: {0}", ((Attribute)obj).getPersistentAttributeType());
}
}
EntityType<Item> item = metaModel.entity(Item.class);
}
示例13: isPathJoin
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
private static boolean isPathJoin(Path<?> path) {
//todo how to avoid this?
/*
if (path instanceof PluralAttributePath) {
return true;
}
*/
if (Collection.class.isAssignableFrom(path.getJavaType()) || path.getJavaType().isArray()) {
return true;
}
Attribute.PersistentAttributeType persistentAttributeType = null;
if (path.getModel() instanceof SingularAttribute) {
persistentAttributeType = ((SingularAttribute) path.getModel()).getPersistentAttributeType();
}
if (persistentAttributeType == Attribute.PersistentAttributeType.MANY_TO_MANY ||
persistentAttributeType == Attribute.PersistentAttributeType.MANY_TO_ONE ||
persistentAttributeType == Attribute.PersistentAttributeType.ONE_TO_MANY ||
persistentAttributeType == Attribute.PersistentAttributeType.ONE_TO_ONE) {
return true;
}
return false;
}
示例14: toAttributes
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
public List<Attribute<?, ?>> toAttributes(String path, Class<?> from) {
try {
List<Attribute<?, ?>> attributes = newArrayList();
Class<?> current = from;
for (String pathItem : Splitter.on(".").split(path)) {
Class<?> metamodelClass = getCachedClass(current);
Field field = metamodelClass.getField(pathItem);
Attribute<?, ?> attribute = (Attribute<?, ?>) field.get(null);
attributes.add(attribute);
if (attribute instanceof PluralAttribute) {
current = ((PluralAttribute<?, ?, ?>) attribute).getElementType().getJavaType();
} else {
current = attribute.getJavaType();
}
}
return attributes;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
示例15: getPath
import javax.persistence.metamodel.Attribute; //导入依赖的package包/类
@SuppressWarnings("unchecked")
public <E, F> Path<F> getPath(Root<E> root, List<Attribute<?, ?>> attributes) {
Path<?> path = root;
for (Attribute<?, ?> attribute : attributes) {
boolean found = false;
if (path instanceof FetchParent) {
for (Fetch<E, ?> fetch : ((FetchParent<?, E>) path).getFetches()) {
if (attribute.getName().equals(fetch.getAttribute().getName()) && (fetch instanceof Join<?, ?>)) {
path = (Join<E, ?>) fetch;
found = true;
break;
}
}
}
if (!found) {
if (attribute instanceof PluralAttribute) {
path = ((From<?, ?>) path).join(attribute.getName(), JoinType.LEFT);
} else {
path = path.get(attribute.getName());
}
}
}
return (Path<F>) path;
}