Search in sources :

Example 1 with Attribute

use of javax.persistence.metamodel.Attribute in project hibernate-orm by hibernate.

the class AbstractManagedType method getBuilder.

public Builder<X> getBuilder() {
    if (locked) {
        throw new IllegalStateException("Type has been locked");
    }
    return new Builder<X>() {

        @Override
        @SuppressWarnings("unchecked")
        public void addAttribute(Attribute<X, ?> attribute) {
            declaredAttributes.put(attribute.getName(), attribute);
            final Bindable.BindableType bindableType = ((Bindable) attribute).getBindableType();
            switch(bindableType) {
                case SINGULAR_ATTRIBUTE:
                    {
                        declaredSingularAttributes.put(attribute.getName(), (SingularAttribute<X, ?>) attribute);
                        break;
                    }
                case PLURAL_ATTRIBUTE:
                    {
                        declaredPluralAttributes.put(attribute.getName(), (PluralAttribute<X, ?, ?>) attribute);
                        break;
                    }
                default:
                    {
                        throw new AssertionFailure("unknown bindable type: " + bindableType);
                    }
            }
        }
    };
}
Also used : SingularAttribute(javax.persistence.metamodel.SingularAttribute) AssertionFailure(org.hibernate.annotations.common.AssertionFailure) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetAttribute(javax.persistence.metamodel.SetAttribute) CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) Bindable(javax.persistence.metamodel.Bindable)

Example 2 with Attribute

use of javax.persistence.metamodel.Attribute in project hibernate-orm by hibernate.

the class AbstractFromImpl method joinCollection.

@Override
@SuppressWarnings({ "unchecked" })
public <X, Y> CollectionJoin<X, Y> joinCollection(String attributeName, JoinType jt) {
    final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute(attributeName);
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException("Requested attribute was not a collection");
    }
    final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
    if (!PluralAttribute.CollectionType.COLLECTION.equals(pluralAttribute.getCollectionType())) {
        throw new IllegalArgumentException("Requested attribute was not a collection");
    }
    return (CollectionJoin<X, Y>) join((CollectionAttribute) attribute, jt);
}
Also used : CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) CollectionJoin(javax.persistence.criteria.CollectionJoin) CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetAttribute(javax.persistence.metamodel.SetAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute)

Example 3 with Attribute

use of javax.persistence.metamodel.Attribute in project hibernate-orm by hibernate.

the class AbstractFromImpl method joinSet.

@Override
@SuppressWarnings({ "unchecked" })
public <X, Y> SetJoin<X, Y> joinSet(String attributeName, JoinType jt) {
    final Attribute<X, ?> attribute = (Attribute<X, ?>) locateAttribute(attributeName);
    if (!attribute.isCollection()) {
        throw new IllegalArgumentException("Requested attribute was not a set");
    }
    final PluralAttribute pluralAttribute = (PluralAttribute) attribute;
    if (!PluralAttribute.CollectionType.SET.equals(pluralAttribute.getCollectionType())) {
        throw new IllegalArgumentException("Requested attribute was not a set");
    }
    return (SetJoin<X, Y>) join((SetAttribute) attribute, jt);
}
Also used : CollectionAttribute(javax.persistence.metamodel.CollectionAttribute) MapAttribute(javax.persistence.metamodel.MapAttribute) SingularAttribute(javax.persistence.metamodel.SingularAttribute) Attribute(javax.persistence.metamodel.Attribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetAttribute(javax.persistence.metamodel.SetAttribute) ListAttribute(javax.persistence.metamodel.ListAttribute) PluralAttribute(javax.persistence.metamodel.PluralAttribute) SetJoin(javax.persistence.criteria.SetJoin) SetAttribute(javax.persistence.metamodel.SetAttribute)

Example 4 with Attribute

use of javax.persistence.metamodel.Attribute in project invesdwin-context-persistence by subes.

the class MySqlLoadDataInfile method createQuery.

@SuppressWarnings({ "unchecked", "GuardedBy" })
private String createQuery(final String workFile) {
    final StringBuilder sb = new StringBuilder();
    sb.append("LOAD DATA LOCAL INFILE '");
    sb.append(workFile);
    sb.append("' INTO TABLE ");
    sb.append(genericType.getSimpleName());
    sb.append(" ");
    sb.append("FIELDS TERMINATED BY ',' ");
    sb.append("ENCLOSED BY '\"'  ");
    sb.append("ESCAPED BY '\\\\' ");
    sb.append("LINES TERMINATED BY '\\n' ");
    sb.append("(");
    final EntityManager em = puContext.getEntityManager();
    final EntityType<E> et = (EntityType<E>) em.getMetamodel().entity(genericType);
    final Set<Attribute<? super E, ?>> attrs = et.getAttributes();
    boolean first = true;
    final List<String> booleanColumns = new ArrayList<String>();
    for (final Attribute<? super E, ?> attr : attrs) {
        if (skipColumn(attr.getJavaMember().getName())) {
            continue;
        }
        if (!first) {
            sb.append(", ");
        }
        first = false;
        final String sqlName = Attributes.extractNativeSqlColumnName(attr);
        // handling boolean properly http://stackoverflow.com/questions/15683809/load-data-from-csv-inside-bit-field-in-mysql
        if (Reflections.isBoolean(attr.getJavaType())) {
            sb.append("@");
            booleanColumns.add(sqlName);
        }
        sb.append(sqlName);
    }
    sb.append(")");
    if (!booleanColumns.isEmpty()) {
        // set valore=cast(@valore as signed);
        sb.append(" set ");
        boolean firstBooleanColumn = true;
        for (final String booleanColumn : booleanColumns) {
            if (!firstBooleanColumn) {
                sb.append(",");
            }
            firstBooleanColumn = false;
            sb.append(booleanColumn);
            sb.append("=cast(@");
            sb.append(booleanColumn);
            sb.append(" as signed)");
        }
    }
    return sb.toString();
}
Also used : EntityType(javax.persistence.metamodel.EntityType) EntityManager(javax.persistence.EntityManager) Attribute(javax.persistence.metamodel.Attribute) ArrayList(java.util.ArrayList)

Example 5 with Attribute

use of javax.persistence.metamodel.Attribute in project invesdwin-context-persistence by subes.

the class MySqlLoadDataInfile method determineJavaColumnNames.

@SuppressWarnings("unchecked")
private List<String> determineJavaColumnNames(final PersistenceUnitContext puContext) {
    final List<String> javaColumnNames = new ArrayList<String>();
    final EntityManager em = puContext.getEntityManager();
    final EntityType<E> et = (EntityType<E>) em.getMetamodel().entity(genericType);
    final Set<Attribute<? super E, ?>> attrs = et.getAttributes();
    for (final Attribute<? super E, ?> attr : attrs) {
        final String javaName = attr.getJavaMember().getName();
        javaColumnNames.add(javaName);
    }
    return javaColumnNames;
}
Also used : EntityType(javax.persistence.metamodel.EntityType) EntityManager(javax.persistence.EntityManager) Attribute(javax.persistence.metamodel.Attribute) ArrayList(java.util.ArrayList)

Aggregations

Attribute (javax.persistence.metamodel.Attribute)31 SingularAttribute (javax.persistence.metamodel.SingularAttribute)21 ListAttribute (javax.persistence.metamodel.ListAttribute)13 PluralAttribute (javax.persistence.metamodel.PluralAttribute)12 MapAttribute (javax.persistence.metamodel.MapAttribute)10 Metamodel (javax.persistence.metamodel.Metamodel)9 CollectionAttribute (javax.persistence.metamodel.CollectionAttribute)6 SetAttribute (javax.persistence.metamodel.SetAttribute)6 Path (javax.persistence.criteria.Path)5 EntityType (javax.persistence.metamodel.EntityType)5 ManagedType (javax.persistence.metamodel.ManagedType)5 EntityManager (javax.persistence.EntityManager)4 TypedQuery (javax.persistence.TypedQuery)4 Predicate (javax.persistence.criteria.Predicate)4 Field (java.lang.reflect.Field)3 Expression (javax.persistence.criteria.Expression)3 From (javax.persistence.criteria.From)3 Root (javax.persistence.criteria.Root)3 IdentifiableType (javax.persistence.metamodel.IdentifiableType)3 Type (javax.persistence.metamodel.Type)3