Example usage for javax.persistence FetchType EAGER

List of usage examples for javax.persistence FetchType EAGER

Introduction

In this page you can find the example usage for javax.persistence FetchType EAGER.

Prototype

FetchType EAGER

To view the source code for javax.persistence FetchType EAGER.

Click Source Link

Document

Defines that data must be eagerly fetched.

Usage

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @Basic. Given annotation may be null.
 *//*  w w  w  .  j a v  a2  s .c o m*/
private void parseBasic(FieldMetaData fmd, Basic anno, boolean lob) {
    Class<?> type = fmd.getDeclaredType();
    if (lob && type != String.class && type != char[].class && type != Character[].class && type != byte[].class
            && type != Byte[].class)
        fmd.setSerialized(true);
    else if (!lob) {
        switch (fmd.getDeclaredTypeCode()) {
        case JavaTypes.OBJECT:
            if (Enum.class.isAssignableFrom(type))
                break;
            // else no break
        case JavaTypes.COLLECTION:
        case JavaTypes.MAP:
        case JavaTypes.PC:
        case JavaTypes.PC_UNTYPED:
            if (Serializable.class.isAssignableFrom(type))
                fmd.setSerialized(true);
            else
                throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "Basic"));
            break;
        case JavaTypes.ARRAY:
            if (type == char[].class || type == Character[].class || type == byte[].class
                    || type == Byte[].class)
                break;
            if (Serializable.class.isAssignableFrom(type.getComponentType()))
                fmd.setSerialized(true);
            else
                throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "Basic"));
            break;
        }
    }

    if (anno == null)
        return;
    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (!anno.optional())
        fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @ManyToOne.//from  w ww  . j av a  2 s  .  c om
 */
private void parseManyToOne(FieldMetaData fmd, ManyToOne anno) {
    if (!JavaTypes.maybePC(fmd.getValue()))
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "ManyToOne"));

    // don't specifically exclude relation from DFG b/c that will prevent
    // us from even reading the fk when reading from the primary table,
    // which is not what most users will want
    if (anno.fetch() == FetchType.EAGER)
        fmd.setInDefaultFetchGroup(true);
    if (!anno.optional())
        fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);
    if (anno.targetEntity() != void.class)
        fmd.setTypeOverride(anno.targetEntity());
    setCascades(fmd, anno.cascade());
    fmd.setAssociationType(FieldMetaData.MANY_TO_ONE);
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @OneToOne./*from ww w. j  ava2  s .  c om*/
 */
private void parseOneToOne(FieldMetaData fmd, OneToOne anno) {
    if (!JavaTypes.maybePC(fmd.getValue()))
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "OneToOne"));

    // don't specifically exclude relation from DFG b/c that will prevent
    // us from even reading the fk when reading from the primary table,
    // which is not what most users will want
    if (anno.fetch() == FetchType.EAGER)
        fmd.setInDefaultFetchGroup(true);
    if (!anno.optional())
        fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);

    if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy()))
        fmd.setMappedBy(anno.mappedBy());
    if (anno.targetEntity() != void.class)
        fmd.setTypeOverride(anno.targetEntity());
    setCascades(fmd, anno.cascade());
    setOrphanRemoval(fmd, anno.orphanRemoval());
    fmd.setAssociationType(FieldMetaData.ONE_TO_ONE);
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @OneToMany.//from w  w w.j  a  va  2s .  c o m
 */
private void parseOneToMany(FieldMetaData fmd, OneToMany anno) {
    switch (fmd.getDeclaredTypeCode()) {
    case JavaTypes.ARRAY:
    case JavaTypes.COLLECTION:
    case JavaTypes.MAP:
        if (JavaTypes.maybePC(fmd.getElement()))
            break;
        // no break
    default:
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "OneToMany"));
    }

    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy()))
        fmd.setMappedBy(anno.mappedBy());
    if (anno.targetEntity() != void.class)
        fmd.getElement().setDeclaredType(anno.targetEntity());
    setCascades(fmd.getElement(), anno.cascade());
    setOrphanRemoval(fmd.getElement(), anno.orphanRemoval());
    fmd.setAssociationType(FieldMetaData.ONE_TO_MANY);
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @ManyToMany.//from w  w  w  .  j a va  2 s  . c om
 */
private void parseManyToMany(FieldMetaData fmd, ManyToMany anno) {
    switch (fmd.getDeclaredTypeCode()) {
    case JavaTypes.ARRAY:
    case JavaTypes.COLLECTION:
    case JavaTypes.MAP:
        if (fmd.getDeclaredType() == Properties.class || JavaTypes.maybePC(fmd.getElement()))
            break;
        // no break
    default:
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "ManyToMany"));
    }

    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (isMappingOverrideMode() && !StringUtils.isEmpty(anno.mappedBy()))
        fmd.setMappedBy(anno.mappedBy());
    if (anno.targetEntity() != void.class)
        fmd.getElement().setDeclaredType(anno.targetEntity());
    setCascades(fmd.getElement(), anno.cascade());
    fmd.setAssociationType(FieldMetaData.MANY_TO_MANY);
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @Persistent.//from  ww w.  j a v a2s .co m
 */
private void parsePersistent(FieldMetaData fmd, Persistent anno) {
    switch (fmd.getDeclaredTypeCode()) {
    case JavaTypes.ARRAY:
        if (fmd.getDeclaredType() == byte[].class || fmd.getDeclaredType() == Byte[].class
                || fmd.getDeclaredType() == char[].class || fmd.getDeclaredType() == Character[].class)
            break;
        // no break
    case JavaTypes.COLLECTION:
    case JavaTypes.MAP:
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "Persistent"));
    }

    if (!StringUtils.isEmpty(anno.mappedBy()))
        fmd.setMappedBy(anno.mappedBy());
    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (!anno.optional())
        fmd.setNullValue(FieldMetaData.NULL_EXCEPTION);
    setCascades(fmd, anno.cascade());
    if (anno.embedded()) {
        if (!JavaTypes.maybePC(fmd.getValue()))
            throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "Persistent(embedded=true)"));
        fmd.setEmbedded(true);
        if (fmd.getEmbeddedMetaData() == null) {
            fmd.addEmbeddedMetaData(getAccessCode(fmd.getDeclaredType()));
        }
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @PersistentCollection./*ww  w  . jav a 2 s . c  o  m*/
 */
private void parsePersistentCollection(FieldMetaData fmd, PersistentCollection anno) {
    if (fmd.getDeclaredTypeCode() != JavaTypes.ARRAY && fmd.getDeclaredTypeCode() != JavaTypes.COLLECTION)
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "PersistentCollection"));

    fmd.setPersistentCollection(true);

    if (!StringUtils.isEmpty(anno.mappedBy()))
        fmd.setMappedBy(anno.mappedBy());
    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (anno.elementType() != void.class)
        fmd.getElement().setDeclaredType(anno.elementType());
    setCascades(fmd.getElement(), anno.elementCascade());
    if (anno.elementEmbedded()) {
        if (!JavaTypes.maybePC(fmd.getElement()))
            throw new MetaDataException(
                    _loc.get("bad-meta-anno", fmd, "PersistentCollection(embeddedElement=true)"));
        fmd.getElement().setEmbedded(true);
        if (fmd.getElement().getEmbeddedMetaData() == null) {
            fmd.getElement().addEmbeddedMetaData(getAccessCode(fmd.getElement().getDeclaredType()));
        }
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @ElementCollection.//from w  w  w. j  a  v  a2 s  .  com
 */
private void parseElementCollection(FieldMetaData fmd, ElementCollection anno) {
    // TODO: throw exception if the runtime env is OpenJpa 1.x

    if (fmd.getDeclaredTypeCode() != JavaTypes.COLLECTION && fmd.getDeclaredTypeCode() != JavaTypes.MAP)
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "ElementCollection"));

    if (anno.targetClass() != void.class)
        fmd.getElement().setDeclaredType(anno.targetClass());
    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    fmd.setElementCollection(true);
    ValueMetaData elem = fmd.getElement();
    boolean isEnum = elem.getDeclaredType().isEnum();
    if (!isEnum && JavaTypes.maybePC(elem)) {
        elem.setEmbedded(true);
        if (elem.getEmbeddedMetaData() == null)
            elem.addEmbeddedMetaData(getAccessCode(elem.getDeclaredType()));
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataParser.java

/**
 * Parse @PersistentMap./*from   w ww.ja  v a 2  s. c o m*/
 */
private void parsePersistentMap(FieldMetaData fmd, PersistentMap anno) {
    if (fmd.getDeclaredTypeCode() != JavaTypes.MAP)
        throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "PersistentMap"));

    fmd.setInDefaultFetchGroup(anno.fetch() == FetchType.EAGER);
    if (anno.keyType() != void.class)
        fmd.getKey().setDeclaredType(anno.keyType());
    if (anno.elementType() != void.class)
        fmd.getElement().setDeclaredType(anno.elementType());
    setCascades(fmd.getKey(), anno.keyCascade());
    setCascades(fmd.getElement(), anno.elementCascade());
    if (anno.keyEmbedded()) {
        if (!JavaTypes.maybePC(fmd.getKey()))
            throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "PersistentMap(embeddedKey=true)"));
        fmd.getKey().setEmbedded(true);
        if (fmd.getKey().getEmbeddedMetaData() == null) {
            fmd.getKey().addEmbeddedMetaData(getAccessCode(fmd.getKey().getDeclaredType()));
        }
    }
    if (anno.elementEmbedded()) {
        if (!JavaTypes.maybePC(fmd.getElement()))
            throw new MetaDataException(_loc.get("bad-meta-anno", fmd, "PersistentMap(embeddedValue=true)"));
        fmd.getElement().setEmbedded(true);
        if (fmd.getElement().getEmbeddedMetaData() == null)
            fmd.getElement().addEmbeddedMetaData(getAccessCode(fmd.getElement().getDeclaredType()));
    }
}

From source file:org.apache.openjpa.persistence.AnnotationPersistenceMetaDataSerializer.java

/**
 * Add one-to-many attributes.//from www .  ja va  2s  .  c o  m
 */
private void addOneToManyAttributes(FieldMetaData fmd, AnnotationBuilder ab) {
    if (fmd.isInDefaultFetchGroup())
        ab.add("fetch", FetchType.EAGER);
    addTargetEntityAttribute(fmd, ab);
}