javax.persistence.metamodel.Metamodel.getManagedTypes()方法的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(11.5k)|赞(0)|评价(0)|浏览(178)

本文整理了Java中javax.persistence.metamodel.Metamodel.getManagedTypes()方法的一些代码示例,展示了Metamodel.getManagedTypes()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Metamodel.getManagedTypes()方法的具体详情如下:
包路径:javax.persistence.metamodel.Metamodel
类名称:Metamodel
方法名:getManagedTypes

Metamodel.getManagedTypes介绍

[英]Return the metamodel managed types.
[中]返回元模型托管类型。

代码示例

代码示例来源:origin: spring-projects/spring-data-jpa

/**
 * Creates a new {@link JpaMetamodel} for the given JPA {@link Metamodel}.
 *
 * @param metamodel must not be {@literal null}.
 */
private JpaMetamodel(Metamodel metamodel) {
  Assert.notNull(metamodel, "Metamodel must not be null!");
  this.metamodel = metamodel;
  this.managedTypes = Lazy.of(() -> metamodel.getManagedTypes().stream() //
      .map(ManagedType::getJavaType) //
      .filter(it -> it != null) //
      .collect(StreamUtils.toUnmodifiableSet()));
}

代码示例来源:origin: spring-projects/spring-data-jpa

/**
 * Creates a new {@link DefaultJpaContext} for the given {@link Set} of {@link EntityManager}s.
 *
 * @param entityManagers must not be {@literal null}.
 */
public DefaultJpaContext(Set<EntityManager> entityManagers) {
  Assert.notNull(entityManagers, "EntityManagers must not be null!");
  Assert.notEmpty(entityManagers, "EntityManagers must not be empty!");
  this.entityManagers = new LinkedMultiValueMap<Class<?>, EntityManager>();
  for (EntityManager em : entityManagers) {
    for (ManagedType<?> managedType : em.getMetamodel().getManagedTypes()) {
      this.entityManagers.add(managedType.getJavaType(), em);
    }
  }
}

代码示例来源:origin: spring-projects/spring-data-jpa

/**
   * Returns the {@link Metamodel} aware of the given type.
   *
   * @param type must not be {@literal null}.
   * @return can be {@literal null}.
   */
  @Nullable
  private Metamodel getMetamodelFor(Class<?> type) {
    for (Metamodel model : metamodels) {
      try {
        model.managedType(type);
        return model;
      } catch (IllegalArgumentException o_O) {
        // Fall back to inspect *all* managed types manually as Metamodel.managedType(…) only
        // returns for entities, embeddables and managed supperclasses.
        for (ManagedType<?> managedType : model.getManagedTypes()) {
          if (type.equals(managedType.getJavaType())) {
            return model;
          }
        }
      }
    }
    return null;
  }
}

代码示例来源:origin: stackoverflow.com

public MySQLSchemaGenerator() throws ClassNotFoundException {
 Properties mySQLDialectProps = new Properties();
 mySQLDialectProps.setProperty("javax.persistence.transactionType", "RESOURCE_LOCAL");
 mySQLDialectProps.setProperty("javax.persistence.jtaDataSource", "");
 final EntityManagerFactory emf = Persistence.createEntityManagerFactory("<persistence_unit_name>", mySQLDialectProps);
 final Metamodel mm = emf.getMetamodel();
 for (final ManagedType<?> managedType : mm.getManagedTypes()) {
  managedType.getJavaType(); // this returns the java class of the @Entity object
 }
}

代码示例来源:origin: Blazebit/blaze-persistence

@Override
public Set<ManagedType<?>> getManagedTypes() {
  return delegate.getManagedTypes();
}

代码示例来源:origin: org.fornax.cartridges/fornax-cartridges-sculptor-framework

@SuppressWarnings({ "unused" })
private boolean isManagedType(Class<?> type) {
  for (ManagedType<?> managedType : metaModel.getManagedTypes()) {
    if (managedType.getJavaType().equals(type)) {
      return true;
    }
  }
  return false;
}

代码示例来源:origin: org.teiid.connectors/translator-jpa

@Override
public Set<ManagedType<?>> getManagedTypes() {
  return model.getManagedTypes().stream()
      .filter(e -> e.getJavaType() != null)
      .collect(Collectors.toSet());
}

代码示例来源:origin: org.springframework.data/spring-data-jpa

/**
 * Creates a new {@link JpaMetamodel} for the given JPA {@link Metamodel}.
 *
 * @param metamodel must not be {@literal null}.
 */
private JpaMetamodel(Metamodel metamodel) {
  Assert.notNull(metamodel, "Metamodel must not be null!");
  this.metamodel = metamodel;
  this.managedTypes = Lazy.of(() -> metamodel.getManagedTypes().stream() //
      .map(ManagedType::getJavaType) //
      .filter(it -> it != null) //
      .collect(StreamUtils.toUnmodifiableSet()));
}

代码示例来源:origin: org.springframework.data/spring-data-jpa

/**
 * Creates a new {@link DefaultJpaContext} for the given {@link Set} of {@link EntityManager}s.
 *
 * @param entityManagers must not be {@literal null}.
 */
public DefaultJpaContext(Set<EntityManager> entityManagers) {
  Assert.notNull(entityManagers, "EntityManagers must not be null!");
  Assert.notEmpty(entityManagers, "EntityManagers must not be empty!");
  this.entityManagers = new LinkedMultiValueMap<Class<?>, EntityManager>();
  for (EntityManager em : entityManagers) {
    for (ManagedType<?> managedType : em.getMetamodel().getManagedTypes()) {
      this.entityManagers.add(managedType.getJavaType(), em);
    }
  }
}

代码示例来源:origin: org.apache.openjpa/openjpa-jest

/**
 * Encodes the given meta-model into a new XML document according to JEST Domain XML Schema.
 * 
 * @param model a persistent domain model. Must not be null.
 */
public Document encode(Metamodel model) {
  Element root = newDocument(ROOT_ELEMENT_MODEL);
  for (ManagedType<?> t : model.getManagedTypes()) {
    encodeManagedType(t, root);
  }
  return root.getOwnerDocument();
}

代码示例来源:origin: ch.inftec.ju/ju-dbutil

/**
 * Helper function that returns all managed types (i.e. entities) of the specified EntityManager
 * as a list of Java Class objects.
 * @param em
 * @return List of Class<?> objects of the DB entity classes (annotated with @Entity)
 */
public static List<Class<?>> getManagedTypesAsClass(EntityManager em) {
  List<Class<?>> classes = new ArrayList<>();
  
  for (ManagedType<?> t : em.getMetamodel().getManagedTypes()) {
    Class<?> clazz = t.getJavaType();
    classes.add(clazz);
  }
  
  return classes;
}

代码示例来源:origin: dzinot/spring-boot-jpa-data-rest-soft-delete

private List<Class<?>> getAllManagedEntityTypes(EntityManagerFactory entityManagerFactory) {
  List<Class<?>> entityClasses = new ArrayList<>();
  Metamodel metamodel = entityManagerFactory.getMetamodel();
  for (ManagedType<?> managedType : metamodel.getManagedTypes())
    if (managedType.getJavaType().isAnnotationPresent(Entity.class))
      entityClasses.add(managedType.getJavaType());
  return entityClasses;
}

代码示例来源:origin: org.openengsb.labs.jpatest/labs-jpatest-core

private void clearTables(EntityManagerFactory emf) {
  Set<ManagedType<?>> types = emf.getMetamodel().getManagedTypes();
  EntityManager entityManager = makeEntityManager(emf);
  for (ManagedType<?> type : types) {
    Class<?> javaType = type.getJavaType();
    String name = retrieveEntityName(javaType);
    if (name == null) {
      LOGGER.warn("could not determine name for entity {}", type);
      continue;
    }
    entityManager.getTransaction().begin();
    entityManager.createQuery("DELETE FROM " + name).executeUpdate();
    entityManager.getTransaction().commit();
  }
  entityManager.close();
}

代码示例来源:origin: org.kuali.rice/rice-krad-data

/**
 * {@inheritDoc}
 */
@Override
public boolean handles(final Class<?> type) {
  if (managedTypesCache == null) {
    managedTypesCache = new HashSet<Class<?>>();
    Set<ManagedType<?>> managedTypes = sharedEntityManager.getMetamodel().getManagedTypes();
    for (ManagedType managedType : managedTypes) {
      managedTypesCache.add(managedType.getJavaType());
    }
  }
  if (managedTypesCache.contains(type)) {
    return true;
  } else {
    return false;
  }
}

代码示例来源:origin: org.springframework.data/spring-data-jpa

/**
   * Returns the {@link Metamodel} aware of the given type.
   *
   * @param type must not be {@literal null}.
   * @return can be {@literal null}.
   */
  @Nullable
  private Metamodel getMetamodelFor(Class<?> type) {
    for (Metamodel model : metamodels) {
      try {
        model.managedType(type);
        return model;
      } catch (IllegalArgumentException o_O) {
        // Fall back to inspect *all* managed types manually as Metamodel.managedType(…) only
        // returns for entities, embeddables and managed supperclasses.
        for (ManagedType<?> managedType : model.getManagedTypes()) {
          if (type.equals(managedType.getJavaType())) {
            return model;
          }
        }
      }
    }
    return null;
  }
}

代码示例来源:origin: com.mysema.querydsl/querydsl-jpa-codegen

@Override
protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException,
  NoSuchMethodException {
  Map<ManagedType<?>, EntityType> types = Maps.newHashMap();
  for (ManagedType<?> managedType : configuration.getManagedTypes()) {
    if (managedType instanceof MappedSuperclassType) {
      types.put(managedType, createSuperType(managedType.getJavaType()));
    } else if (managedType instanceof javax.persistence.metamodel.EntityType) {
      types.put(managedType, createEntityType(managedType.getJavaType()));
    } else if (managedType instanceof EmbeddableType) {
      types.put(managedType, createEmbeddableType(managedType.getJavaType()));
    } else {
      throw new IllegalArgumentException("Unknown type " + managedType);
    }
  }
  // handle properties
  for (Map.Entry<ManagedType<?>, EntityType> entry : types.entrySet()) {
    EntityType entityType = entry.getValue();
    for (Attribute<?,?> attribute : entry.getKey().getDeclaredAttributes()) {
      handleProperty(entityType, entityType.getJavaClass(), attribute);
    }
  }
}

代码示例来源:origin: com.querydsl/querydsl-jpa-codegen

@Override
protected void collectTypes() throws IOException, XMLStreamException, ClassNotFoundException,
  NoSuchMethodException {
  Map<ManagedType<?>, EntityType> types = Maps.newHashMap();
  for (ManagedType<?> managedType : metamodel.getManagedTypes()) {
    if (managedType instanceof MappedSuperclassType) {
      types.put(managedType, createSuperType(managedType.getJavaType()));
    } else if (managedType instanceof javax.persistence.metamodel.EntityType) {
      types.put(managedType, createEntityType(managedType.getJavaType()));
    } else if (managedType instanceof EmbeddableType) {
      types.put(managedType, createEmbeddableType(managedType.getJavaType()));
    } else {
      throw new IllegalArgumentException("Unknown type " + managedType);
    }
  }
  // handle properties
  for (Map.Entry<ManagedType<?>, EntityType> entry : types.entrySet()) {
    EntityType entityType = entry.getValue();
    for (Attribute<?,?> attribute : entry.getKey().getDeclaredAttributes()) {
      handleProperty(entityType, entityType.getJavaClass(), attribute);
    }
  }
}

代码示例来源:origin: liquibase/liquibase-hibernate

/**
 * Adds sources based on what is in the saved entityManagerFactory
 */
@Override
protected void configureSources(MetadataSources sources) throws DatabaseException {
  for (ManagedType<?> managedType : entityManagerFactory.getMetamodel().getManagedTypes()) {
    Class<?> javaType = managedType.getJavaType();
    if (javaType == null) {
      continue;
    }
    sources.addAnnotatedClass(javaType);
  }
  Package[] packages = Package.getPackages();
  for (Package p : packages) {
    sources.addPackage(p);
  }
}

代码示例来源:origin: net.e6tech/elements-persist

public void initialize(Resources resources) {
  startMonitoring();
  emf = Persistence.createEntityManagerFactory(persistenceUnitName, persistenceProperties);
  EntityManager em = null;
  try {
    em = emf.createEntityManager();
    Metamodel meta = emf.getMetamodel();
    meta.getManagedTypes().forEach(type -> {
      type.getDeclaredAttributes();
      type.getPersistenceType();
    });
  } finally {
    if (em != null)
      em.close();
  }
  NotificationCenter center = resources.getNotificationCenter();
  center.subscribe(EvictCollectionRegion.class,
      notice -> evictCollectionRegion(notice.getUserObject()));
  center.subscribe(EvictEntityRegion.class,
      notice -> evictEntityRegion(notice.getUserObject()));
  center.subscribe(EvictEntity.class,
      notice -> evictEntity(notice.getUserObject()));
}

代码示例来源:origin: katharsis-project/katharsis-framework

/**
 * Constructor used on server side.
 */
private JpaModule(EntityManagerFactory emFactory, EntityManager em, TransactionRunner transactionRunner) {
  this();
  this.emFactory = emFactory;
  this.em = em;
  this.transactionRunner = transactionRunner;
  setQueryFactory(JpaCriteriaQueryFactory.newInstance());
  if (emFactory != null) {
    Set<ManagedType<?>> managedTypes = emFactory.getMetamodel().getManagedTypes();
    for (ManagedType<?> managedType : managedTypes) {
      Class<?> managedJavaType = managedType.getJavaType();
      MetaElement meta = jpaMetaLookup.getMeta(managedJavaType, MetaJpaDataObject.class);
      if (meta instanceof MetaEntity) {
        addRepository(JpaRepositoryConfig.builder(managedJavaType).build());
      }
    }
  }
  this.setRepositoryFactory(new DefaultJpaRepositoryFactory());
}

相关文章