use of javax.persistence.metamodel.Metamodel in project querydsl by querydsl.
the class AntJPADomainExporter method execute.
/**
* Exports the named persistence unit's metamodel to Querydsl query types. Expects to be
* called by Ant via name convention using a method with signature public void execute().
*/
public void execute() {
// We can assume we have the named persistence unit and its mapping file in our classpath,
// but we may have to allow some properties in that persistence unit to be overridden before
// we can successfully get that persistence unit's metamodel.
Map<String, String> properties = (configuration != null) ? configuration.getProperties() : null;
EntityManagerFactory emf = Persistence.createEntityManagerFactory(persistenceUnitName, properties);
// Now we can get the persistence unit's metamodel and export it to Querydsl query types.
Metamodel configuration = emf.getMetamodel();
JPADomainExporter exporter = new JPADomainExporter(namePrefix, nameSuffix, new File(targetFolder), configuration);
try {
exporter.execute();
generatedFiles = exporter.getGeneratedFiles();
} catch (IOException e) {
throw new RuntimeException("Error in JPADomainExporter", e);
}
}
use of javax.persistence.metamodel.Metamodel in project uPortal by Jasig.
the class BaseJpaDaoTest method deleteAllEntities.
/** Deletes ALL entities from the database */
@After
public final void deleteAllEntities() {
final EntityManager entityManager = getEntityManager();
final EntityManagerFactory entityManagerFactory = entityManager.getEntityManagerFactory();
final Metamodel metamodel = entityManagerFactory.getMetamodel();
Set<EntityType<?>> entityTypes = new LinkedHashSet<EntityType<?>>(metamodel.getEntities());
do {
final Set<EntityType<?>> failedEntitieTypes = new HashSet<EntityType<?>>();
for (final EntityType<?> entityType : entityTypes) {
final String entityClassName = entityType.getBindableJavaType().getName();
try {
this.executeInTransaction(new CallableWithoutResult() {
@Override
protected void callWithoutResult() {
logger.trace("Purging all: " + entityClassName);
final Query query = entityManager.createQuery("SELECT e FROM " + entityClassName + " AS e");
final List<?> entities = query.getResultList();
logger.trace("Found " + entities.size() + " " + entityClassName + " to delete");
for (final Object entity : entities) {
entityManager.remove(entity);
}
}
});
} catch (DataIntegrityViolationException e) {
logger.trace("Failed to delete " + entityClassName + ". Must be a dependency of another entity");
failedEntitieTypes.add(entityType);
}
}
entityTypes = failedEntitieTypes;
} while (!entityTypes.isEmpty());
//Reset all spring managed mocks after every test
MockitoFactoryBean.resetAllMocks();
}
use of javax.persistence.metamodel.Metamodel in project webpieces by deanhiller.
the class HibernateLookup method find.
@SuppressWarnings("unchecked")
@Override
public <T> T find(Meta paramMeta, ParamTreeNode tree, Function<Class<T>, T> beanCreate) {
if (!(paramMeta instanceof ParamMeta))
throw new UnsupportedOperationException("this plugin does not support type=" + paramMeta.getClass());
ParamMeta m = (ParamMeta) paramMeta;
Class<T> paramTypeToCreate = (Class<T>) m.getFieldClass();
EntityManager entityManager = Em.get();
Metamodel metamodel = entityManager.getMetamodel();
ManagedType<T> managedType = metamodel.managedType(paramTypeToCreate);
EntityTypeImpl<T> entityType = (EntityTypeImpl<T>) managedType;
Class<?> idClazz = entityType.getIdType().getJavaType();
SingularAttribute<? super T, ?> idAttribute = entityType.getId(idClazz);
String name = idAttribute.getName();
ParamNode paramNode = tree.get(name);
String value = null;
if (paramNode != null) {
if (!(paramNode instanceof ValueNode))
throw new IllegalStateException("The id field in the hibernate entity should have matched to a " + "ValueNode on incoming data and did not. node=" + paramNode + ". bad multipart form? (Please " + "let us know so we can pair with you on this and I can add better error messaging)");
ValueNode node = (ValueNode) paramNode;
value = node.getValue();
}
if (value == null)
return beanCreate.apply(paramTypeToCreate);
@SuppressWarnings("rawtypes") ObjectStringConverter unmarshaller = translator.getConverter(idClazz);
Object id = unmarshaller.stringToObject(value);
UseQuery namedQuery = fetchUseQuery(m.getAnnotations());
if (namedQuery == null)
return entityManager.find(paramTypeToCreate, id);
Query query = entityManager.createNamedQuery(namedQuery.value());
query.setParameter(namedQuery.id(), id);
return (T) query.getSingleResult();
}
Aggregations