Search in sources :

Example 1 with FetchType

use of jakarta.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 @ElementCollection");
    }
    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.AssertionFailure) LazyCollection(org.hibernate.annotations.LazyCollection) FetchType(jakarta.persistence.FetchType) ManyToMany(jakarta.persistence.ManyToMany) ElementCollection(jakarta.persistence.ElementCollection) OneToMany(jakarta.persistence.OneToMany)

Example 2 with FetchType

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

the class AnnotationBinder method defineFetchingStrategy.

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(fetchType != FetchType.LAZY);
        toOne.setUnwrapProxyImplicit(true);
    }
    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(jakarta.persistence.OneToOne) AssertionFailure(org.hibernate.AssertionFailure) FetchType(jakarta.persistence.FetchType) AnnotationException(org.hibernate.AnnotationException) ManyToOne(jakarta.persistence.ManyToOne) LazyToOne(org.hibernate.annotations.LazyToOne)

Aggregations

FetchType (jakarta.persistence.FetchType)2 AssertionFailure (org.hibernate.AssertionFailure)2 Fetch (org.hibernate.annotations.Fetch)2 ElementCollection (jakarta.persistence.ElementCollection)1 ManyToMany (jakarta.persistence.ManyToMany)1 ManyToOne (jakarta.persistence.ManyToOne)1 OneToMany (jakarta.persistence.OneToMany)1 OneToOne (jakarta.persistence.OneToOne)1 AnnotationException (org.hibernate.AnnotationException)1 LazyCollection (org.hibernate.annotations.LazyCollection)1 LazyToOne (org.hibernate.annotations.LazyToOne)1 ManyToAny (org.hibernate.annotations.ManyToAny)1