use of javax.persistence.metamodel.SingularAttribute in project teiid by teiid.
the class JPAMetadataProcessor method addSingularAttributes.
private void addSingularAttributes(MetadataFactory mf, Metamodel model, ManagedType<?> entity, Table entityTable, List<String> path) throws TranslatorException {
List<Attribute<?, ?>> attributes = new ArrayList<>(entity.getAttributes());
Collections.sort(attributes, Comparator.comparing(Attribute::getName));
for (Attribute<?, ?> attr : attributes) {
if (!attr.isCollection()) {
List<String> attrPath = new LinkedList<>(path);
attrPath.add(attr.getName());
boolean simpleType = isSimpleType(attr.getJavaType());
if (simpleType) {
Column column = addColumn(mf, String.join("_", attrPath), TypeFacility.getDataTypeName(getJavaDataType(attr.getJavaType())), entityTable);
if (((SingularAttribute) attr).isOptional()) {
column.setDefaultValue(null);
}
column.setNameInSource(String.join(".", attrPath));
} else if (attr.getJavaType().isEnum()) {
Column column = addColumn(mf, String.join("_", attrPath), DataTypeManager.DefaultDataTypes.STRING, entityTable);
if (((SingularAttribute) attr).isOptional()) {
column.setDefaultValue(null);
}
column.setNativeType(attr.getJavaType().getName());
column.setNameInSource(String.join(".", attrPath));
} else {
boolean classFound = false;
// this tables columns
for (EmbeddableType<?> embeddable : model.getEmbeddables()) {
if (embeddable.getJavaType().equals(attr.getJavaType())) {
addSingularAttributes(mf, model, embeddable, entityTable, attrPath);
classFound = true;
break;
}
}
if (!classFound) {
// table, then add that column as FK
for (EntityType et : model.getEntities()) {
if (et.getJavaType().equals(attr.getJavaType())) {
Table attributeTable = addEntity(mf, model, et);
KeyRecord pk = attributeTable.getPrimaryKey();
if (pk != null) {
// TODO: entities must have PK, so this check is not needed.
ArrayList<String> keys = new ArrayList<String>();
for (Column column : pk.getColumns()) {
String fk = attr.getName() + "_" + column.getName();
Column c = addColumn(mf, fk, column.getDatatype().getRuntimeTypeName(), entityTable);
c.setProperty(RELATION_PROPERTY, attr.getName());
c.setProperty(RELATION_KEY, column.getName());
c.setNameInSource(column.getNameInSource());
keys.add(fk);
}
if (!foreignKeyExists(keys, entityTable)) {
addForeignKey(mf, attr.getName(), keys, attributeTable.getName(), entityTable);
}
} else {
throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14001, attributeTable.getName()));
}
classFound = true;
break;
}
}
}
if (!classFound) {
throw new TranslatorException(JPAPlugin.Util.gs(JPAPlugin.Event.TEIID14002, attr.getName()));
}
}
}
}
}
use of javax.persistence.metamodel.SingularAttribute in project lynx by TFaga.
the class JPAUtils method getEntityIdField.
@SuppressWarnings("unchecked")
private static String getEntityIdField(EntityManager em, Class entity) {
String idProperty = "";
Metamodel metamodel = em.getMetamodel();
EntityType e = metamodel.entity(entity);
Set<SingularAttribute> singularAttributes = e.getSingularAttributes();
for (SingularAttribute singularAttribute : singularAttributes) {
if (singularAttribute.isId()) {
idProperty = singularAttribute.getName();
break;
}
}
return idProperty;
}
use of javax.persistence.metamodel.SingularAttribute in project pnc by project-ncl.
the class EntityRSQLNodeTraveller method getValue.
private Value getValue(Path path, String argument) {
SingularAttribute pathAttribute = ((SingularAttributePath) path).getAttribute();
Class<?> entityClass = pathAttribute.getJavaMember().getDeclaringClass();
return new Value(entityClass, pathAttribute.getName(), path.getJavaType(), argument);
}
use of javax.persistence.metamodel.SingularAttribute in project nrich by croz-ltd.
the class DefaultRegistryConfigurationService method resolveManagedTypePropertyList.
private List<RegistryPropertyConfiguration> resolveManagedTypePropertyList(ManagedType<?> managedType, Class<?> entityType, String prefix, Predicate<String> isIdAttributePredicate, boolean isIdReadOnly, RegistryOverrideConfiguration registryOverrideConfiguration) {
List<String> ignoredPropertyList = Optional.ofNullable(registryOverrideConfiguration.getIgnoredPropertyList()).orElse(Collections.emptyList());
List<String> readOnlyOverridePropertyList = Optional.ofNullable(registryOverrideConfiguration.getNonEditablePropertyList()).orElse(Collections.emptyList());
List<String> nonSortablePropertyList = Optional.ofNullable(registryOverrideConfiguration.getNonSortablePropertyList()).orElse(Collections.emptyList());
List<String> nonSearchablePropertyList = Optional.ofNullable(registryOverrideConfiguration.getNonSearchablePropertyList()).orElse(Collections.emptyList());
List<RegistryPropertyConfiguration> registryPropertyConfigurationList = new ArrayList<>();
managedType.getAttributes().forEach(attribute -> {
if (shouldSkipAttribute(ignoredPropertyList, attribute)) {
return;
}
String attributeName = prefix == null ? attribute.getName() : String.format(RegistryConfigurationConstants.REGISTRY_PROPERTY_PREFIX_FORMAT, prefix, attribute.getName());
Class<?> attributeType = attribute.getJavaType();
boolean isIdAttribute = isIdAttributePredicate.test(attributeName);
boolean isSingularAssociation = attribute.isAssociation() && attribute instanceof SingularAttribute;
Class<?> singularAssociationReferencedClass = isSingularAssociation ? resolveSingularAssociationReferencedClass(attribute) : null;
boolean isReadOnly = isIdAttribute ? isIdReadOnly : readOnlyPropertyList.contains(attributeName) || readOnlyOverridePropertyList.contains(attributeName);
boolean isSortable = !nonSortablePropertyList.contains(attributeName);
boolean isSearchable = !nonSearchablePropertyList.contains(attributeName);
RegistryPropertyConfiguration registryPropertyConfiguration = resolveRegistryPropertyConfiguration(entityType.getName(), attributeType, attributeName, isIdAttribute, isSingularAssociation, singularAssociationReferencedClass, isReadOnly, isSortable, isSearchable);
registryPropertyConfigurationList.add(registryPropertyConfiguration);
});
return registryPropertyConfigurationList;
}
use of javax.persistence.metamodel.SingularAttribute in project tests by datanucleus.
the class CriteriaStringsTest method testBasicWithFromJoinOneToOne.
/**
* Test basic generation of query with candidate and alias, and FROM join on 1-1.
*/
public void testBasicWithFromJoinOneToOne() {
EntityManager em = getEM();
EntityTransaction tx = em.getTransaction();
try {
tx.begin();
CriteriaBuilder cb = emf.getCriteriaBuilder();
CriteriaQuery<Employee> crit = cb.createQuery(Employee.class);
Root<Employee> candidate = crit.from(Employee.class);
candidate.alias("e");
crit.select(candidate);
Metamodel model = emf.getMetamodel();
ManagedType empType = model.managedType(Employee.class);
Attribute bAttr = empType.getAttribute("account");
Join accountJoin = candidate.join((SingularAttribute) bAttr);
accountJoin.alias("a");
// DN extension
assertEquals("Generated JPQL query is incorrect", "SELECT e FROM org.datanucleus.samples.annotations.models.company.Employee e JOIN e.account a", crit.toString());
Query q = em.createQuery(crit);
List<Employee> results = q.getResultList();
assertNotNull("Null results returned!", results);
assertEquals("Number of results is incorrect", 2, results.size());
Iterator<Employee> iter = results.iterator();
boolean joeExists = false;
boolean nigelExists = false;
while (iter.hasNext()) {
Employee emp = iter.next();
if (emp.getFirstName().equals("Nigel") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 106) {
nigelExists = true;
} else if (emp.getFirstName().equals("Joe") && emp.getLastName().equals("Bloggs") && emp.getPersonNum() == 105) {
joeExists = true;
}
}
assertTrue("Nigel not present", nigelExists);
assertTrue("Joe not present", joeExists);
tx.rollback();
} finally {
if (tx.isActive()) {
tx.rollback();
}
em.close();
}
}
Aggregations