Search in sources :

Example 11 with SingularAttribute

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()));
                }
            }
        }
    }
}
Also used : Table(org.teiid.metadata.Table) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) EmbeddableType(javax.persistence.metamodel.EmbeddableType) EntityType(javax.persistence.metamodel.EntityType) KeyRecord(org.teiid.metadata.KeyRecord) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Column(org.teiid.metadata.Column) TranslatorException(org.teiid.translator.TranslatorException)

Example 12 with SingularAttribute

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;
}
Also used : EntityType(javax.persistence.metamodel.EntityType) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Metamodel(javax.persistence.metamodel.Metamodel)

Example 13 with SingularAttribute

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);
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) Value(org.jboss.pnc.facade.rsql.converter.Value) SingularAttributePath(org.hibernate.query.criteria.internal.path.SingularAttributePath)

Example 14 with SingularAttribute

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;
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) RegistryPropertyConfiguration(net.croz.nrich.registry.api.configuration.model.property.RegistryPropertyConfiguration) ArrayList(java.util.ArrayList)

Example 15 with SingularAttribute

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();
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityTransaction(javax.persistence.EntityTransaction) ManagedType(javax.persistence.metamodel.ManagedType) TypedQuery(javax.persistence.TypedQuery) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) ListAttribute(javax.persistence.metamodel.ListAttribute) Join(javax.persistence.criteria.Join) EntityManager(javax.persistence.EntityManager) Employee(org.datanucleus.samples.annotations.models.company.Employee) Metamodel(javax.persistence.metamodel.Metamodel)

Aggregations

SingularAttribute (javax.persistence.metamodel.SingularAttribute)38 Attribute (javax.persistence.metamodel.Attribute)13 Metamodel (javax.persistence.metamodel.Metamodel)13 ListAttribute (javax.persistence.metamodel.ListAttribute)8 ArrayList (java.util.ArrayList)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)7 MapAttribute (javax.persistence.metamodel.MapAttribute)7 Test (org.junit.Test)7 Predicate (javax.persistence.criteria.Predicate)5 EntityType (javax.persistence.metamodel.EntityType)5 Root (javax.persistence.criteria.Root)4 PluralAttribute (javax.persistence.metamodel.PluralAttribute)4 EntityManager (javax.persistence.EntityManager)3 Path (javax.persistence.criteria.Path)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3 VersionedPerson (org.datanucleus.samples.annotations.versioned.VersionedPerson)3 EntityTransaction (javax.persistence.EntityTransaction)2 Query (javax.persistence.Query)2 TypedQuery (javax.persistence.TypedQuery)2