Search in sources :

Example 1 with FetchType

use of javax.persistence.FetchType in project eweb4j-framework by laiweiwei.

the class DAOImpl method queryBySql.

public <T> Collection<T> queryBySql(final String sql) {
    Class<T> mappingCls = null;
    if (this.targetEntity == null)
        mappingCls = (Class<T>) this.clazz;
    else
        mappingCls = (Class<T>) this.targetEntity;
    List<T> result = null;
    try {
        if (Map.class.isAssignableFrom(mappingCls)) {
            Connection con = ds.getConnection();
            if (args != null && args.size() > 0) {
                result = (List<T>) JdbcUtil.getListWithArgs(con, mappingCls, sql, args.toArray(new Object[] {}));
            } else {
                result = (List<T>) JdbcUtil.getList(con, mappingCls, sql);
            }
        } else {
            if (args != null && args.size() > 0) {
                result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql, args.toArray(new Object[] {}));
            } else {
                result = (List<T>) DAOFactory.getSelectDAO(dsName).selectBySQL(mappingCls, sql);
            }
        }
        //this.clear();
        if (result != null && result.size() > 0) {
            for (T t : result) {
                // ToOne relation class cascade select
                ReflectUtil ru = new ReflectUtil(t);
                for (Field field : ru.getFields()) {
                    String f = field.getName();
                    OneToOne o2o = field.getAnnotation(OneToOne.class);
                    ManyToOne m2o = field.getAnnotation(ManyToOne.class);
                    OneToMany o2m = field.getAnnotation(OneToMany.class);
                    ManyToMany m2m = field.getAnnotation(ManyToMany.class);
                    FetchType fetchType = null;
                    boolean is2One = false;
                    if (o2o != null) {
                        fetchType = o2o.fetch();
                        is2One = true;
                    }
                    if (m2o != null) {
                        fetchType = m2o.fetch();
                        is2One = true;
                    }
                    if (o2m != null) {
                        fetchType = o2m.fetch();
                    }
                    if (m2m != null) {
                        fetchType = m2m.fetch();
                    }
                    if (fetchType == null)
                        continue;
                    String beanId = field.getType().getName();
                    if (!is2One)
                        beanId = ClassUtil.getGenericType(field).getName();
                    boolean isEntity = ORMConfigBeanCache.containsKey(beanId);
                    if (!isEntity)
                        continue;
                    if (unFetch.contains(f))
                        continue;
                    if (fetch.contains(f)) {
                        log.debug("cascade select -> " + t.getClass().getName() + "." + f);
                        DAOFactory.getCascadeDAO(dsName).select(t, f);
                        continue;
                    }
                    if (FetchType.LAZY.equals(fetchType))
                        continue;
                    log.debug("cascade select -> " + t.getClass().getName() + "." + f);
                    DAOFactory.getCascadeDAO(dsName).select(t, f);
                }
            }
        }
        return result;
    } catch (Exception e) {
        log.error("sql-->" + sql, e);
        throw new DAOException(sql + " execute exception", e);
    }
}
Also used : FetchType(javax.persistence.FetchType) Connection(java.sql.Connection) ManyToMany(javax.persistence.ManyToMany) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne) SQLException(java.sql.SQLException) ReflectUtil(org.eweb4j.util.ReflectUtil) Field(java.lang.reflect.Field) OneToOne(javax.persistence.OneToOne)

Example 2 with FetchType

use of javax.persistence.FetchType in project hibernate-orm by hibernate.

the class CollectionBinder method defineFetchingStrategy.

private void defineFetchingStrategy() {
    LazyCollection lazy = property.getAnnotation(LazyCollection.class);
    Fetch fetch = property.getAnnotation(Fetch.class);
    OneToMany oneToMany = property.getAnnotation(OneToMany.class);
    ManyToMany manyToMany = property.getAnnotation(ManyToMany.class);
    ElementCollection elementCollection = property.getAnnotation(ElementCollection.class);
    ManyToAny manyToAny = property.getAnnotation(ManyToAny.class);
    FetchType fetchType;
    if (oneToMany != null) {
        fetchType = oneToMany.fetch();
    } else if (manyToMany != null) {
        fetchType = manyToMany.fetch();
    } else if (elementCollection != null) {
        fetchType = elementCollection.fetch();
    } else if (manyToAny != null) {
        fetchType = FetchType.LAZY;
    } else {
        throw new AssertionFailure("Define fetch strategy on a property not annotated with @ManyToOne nor @OneToMany nor @CollectionOfElements");
    }
    if (lazy != null) {
        collection.setLazy(!(lazy.value() == LazyCollectionOption.FALSE));
        collection.setExtraLazy(lazy.value() == LazyCollectionOption.EXTRA);
    } else {
        collection.setLazy(fetchType == FetchType.LAZY);
        collection.setExtraLazy(false);
    }
    if (fetch != null) {
        if (fetch.value() == org.hibernate.annotations.FetchMode.JOIN) {
            collection.setFetchMode(FetchMode.JOIN);
            collection.setLazy(false);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SELECT) {
            collection.setFetchMode(FetchMode.SELECT);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT) {
            collection.setFetchMode(FetchMode.SELECT);
            collection.setSubselectLoadable(true);
            collection.getOwner().setSubselectLoadableCollections(true);
        } else {
            throw new AssertionFailure("Unknown FetchMode: " + fetch.value());
        }
    } else {
        collection.setFetchMode(AnnotationBinder.getFetchMode(fetchType));
    }
}
Also used : Fetch(org.hibernate.annotations.Fetch) ManyToAny(org.hibernate.annotations.ManyToAny) AssertionFailure(org.hibernate.annotations.common.AssertionFailure) LazyCollection(org.hibernate.annotations.LazyCollection) FetchType(javax.persistence.FetchType) ManyToMany(javax.persistence.ManyToMany) ElementCollection(javax.persistence.ElementCollection) OneToMany(javax.persistence.OneToMany)

Example 3 with FetchType

use of javax.persistence.FetchType in project stdlib by petergeneric.

the class QEntity method isEagerFetch.

protected boolean isEagerFetch(final Attribute<?, ?> attribute) {
    if (attribute.isCollection() || attribute.isAssociation()) {
        final Member member = attribute.getJavaMember();
        if (member instanceof AnnotatedElement) {
            final AnnotatedElement el = (AnnotatedElement) member;
            final boolean hasEagerFetch = el.isAnnotationPresent(EagerFetch.class);
            final FetchType fetchType;
            if (el.isAnnotationPresent(OneToMany.class))
                fetchType = el.getAnnotation(OneToMany.class).fetch();
            else if (el.isAnnotationPresent(ManyToOne.class))
                fetchType = el.getAnnotation(ManyToOne.class).fetch();
            else if (el.isAnnotationPresent(OneToOne.class))
                fetchType = el.getAnnotation(OneToOne.class).fetch();
            else if (el.isAnnotationPresent(ElementCollection.class))
                fetchType = el.getAnnotation(ElementCollection.class).fetch();
            else if (el.isAnnotationPresent(ManyToMany.class))
                fetchType = el.getAnnotation(ManyToMany.class).fetch();
            else
                return false;
            if (hasEagerFetch && fetchType == FetchType.EAGER)
                log.warn("ENTITY HAS @EagerFetch but also JPA fetch=EAGER annotation - will not be able to instruct Hibernate to suppress fetching this relation: " + clazz + " " + member.toString());
            return (hasEagerFetch || fetchType == FetchType.EAGER);
        }
    }
    return false;
}
Also used : OneToOne(javax.persistence.OneToOne) FetchType(javax.persistence.FetchType) AnnotatedElement(java.lang.reflect.AnnotatedElement) ManyToMany(javax.persistence.ManyToMany) ElementCollection(javax.persistence.ElementCollection) OneToMany(javax.persistence.OneToMany) Member(java.lang.reflect.Member) ManyToOne(javax.persistence.ManyToOne)

Example 4 with FetchType

use of javax.persistence.FetchType in project jirm by agentgt.

the class SqlParameterDefinition method parameterDef.

static SqlParameterDefinition parameterDef(SqlObjectConfig config, Class<?> objectType, String parameterName, Class<?> parameterType, int order) {
    final SqlParameterDefinition definition;
    String sn = null;
    ManyToOne manyToOne = getAnnotation(objectType, parameterName, ManyToOne.class);
    if (manyToOne != null) {
        Class<?> subK = checkNotNull(manyToOne.targetEntity(), "targetEntity not set");
        JoinColumn joinColumn = getAnnotation(objectType, parameterName, JoinColumn.class);
        SqlObjectDefinition<?> od = SqlObjectDefinition.fromClass(subK, config);
        checkState(!od.getIdParameters().isEmpty(), "No id parameters");
        if (joinColumn != null)
            sn = joinColumn.name();
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        FetchType fetch = manyToOne.fetch();
        int depth;
        if (FetchType.LAZY == fetch) {
            depth = 1;
        } else {
            depth = config.getMaximumLoadDepth();
        }
        SqlParameterObjectDefinition sod = new SqlParameterObjectDefinition(od, depth);
        definition = SqlParameterDefinition.newComplexInstance(config.getConverter(), parameterName, sod, order, sn);
    } else {
        Column col = getAnnotation(objectType, parameterName, Column.class);
        if (col != null && !isNullOrEmpty(col.name()))
            sn = col.name();
        Id id = getAnnotation(objectType, parameterName, Id.class);
        Version version = getAnnotation(objectType, parameterName, Version.class);
        GeneratedValue generated = getAnnotation(objectType, parameterName, GeneratedValue.class);
        Enumerated enumerated = getAnnotation(objectType, parameterName, Enumerated.class);
        boolean idFlag = id != null;
        boolean versionFlag = version != null;
        boolean generatedFlag = generated != null;
        if (sn == null)
            sn = config.getNamingStrategy().propertyToColumnName(parameterName);
        definition = SqlParameterDefinition.newSimpleInstance(config.getConverter(), parameterName, parameterType, order, sn, idFlag, versionFlag, generatedFlag, Optional.fromNullable(enumerated));
    }
    return definition;
}
Also used : FetchType(javax.persistence.FetchType) ManyToOne(javax.persistence.ManyToOne) GeneratedValue(javax.persistence.GeneratedValue) Enumerated(javax.persistence.Enumerated) JoinColumn(javax.persistence.JoinColumn) JoinColumn(javax.persistence.JoinColumn) Column(javax.persistence.Column) Version(javax.persistence.Version) Id(javax.persistence.Id)

Example 5 with FetchType

use of javax.persistence.FetchType in project hibernate-orm by hibernate.

the class AnnotationBinder method defineFetchingStrategy.

protected static void defineFetchingStrategy(ToOne toOne, XProperty property) {
    LazyToOne lazy = property.getAnnotation(LazyToOne.class);
    Fetch fetch = property.getAnnotation(Fetch.class);
    ManyToOne manyToOne = property.getAnnotation(ManyToOne.class);
    OneToOne oneToOne = property.getAnnotation(OneToOne.class);
    FetchType fetchType;
    if (manyToOne != null) {
        fetchType = manyToOne.fetch();
    } else if (oneToOne != null) {
        fetchType = oneToOne.fetch();
    } else {
        throw new AssertionFailure("Define fetch strategy on a property not annotated with @OneToMany nor @OneToOne");
    }
    if (lazy != null) {
        toOne.setLazy(!(lazy.value() == LazyToOneOption.FALSE));
        toOne.setUnwrapProxy((lazy.value() == LazyToOneOption.NO_PROXY));
    } else {
        toOne.setLazy(fetchType == FetchType.LAZY);
        toOne.setUnwrapProxy(false);
    }
    if (fetch != null) {
        if (fetch.value() == org.hibernate.annotations.FetchMode.JOIN) {
            toOne.setFetchMode(FetchMode.JOIN);
            toOne.setLazy(false);
            toOne.setUnwrapProxy(false);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SELECT) {
            toOne.setFetchMode(FetchMode.SELECT);
        } else if (fetch.value() == org.hibernate.annotations.FetchMode.SUBSELECT) {
            throw new AnnotationException("Use of FetchMode.SUBSELECT not allowed on ToOne associations");
        } else {
            throw new AssertionFailure("Unknown FetchMode: " + fetch.value());
        }
    } else {
        toOne.setFetchMode(getFetchMode(fetchType));
    }
}
Also used : Fetch(org.hibernate.annotations.Fetch) OneToOne(javax.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) FetchType(javax.persistence.FetchType) AnnotationException(org.hibernate.AnnotationException) ManyToOne(javax.persistence.ManyToOne) LazyToOne(org.hibernate.annotations.LazyToOne)

Aggregations

FetchType (javax.persistence.FetchType)5 ManyToOne (javax.persistence.ManyToOne)4 ManyToMany (javax.persistence.ManyToMany)3 OneToMany (javax.persistence.OneToMany)3 OneToOne (javax.persistence.OneToOne)3 ElementCollection (javax.persistence.ElementCollection)2 Fetch (org.hibernate.annotations.Fetch)2 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Field (java.lang.reflect.Field)1 Member (java.lang.reflect.Member)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Column (javax.persistence.Column)1 Enumerated (javax.persistence.Enumerated)1 GeneratedValue (javax.persistence.GeneratedValue)1 Id (javax.persistence.Id)1 JoinColumn (javax.persistence.JoinColumn)1 Version (javax.persistence.Version)1 ReflectUtil (org.eweb4j.util.ReflectUtil)1 AnnotationException (org.hibernate.AnnotationException)1