简体   繁体   中英

eclipselink loading n+1 associations even though they are marked as FetchType.LAZY

Given a mapped super class like this.

My query is resulting in each of the mapped classes getting loaded by JPA as soon as getResultsList() is executed.

I am not accessing any of the properties associated with the Order or Vehicle, but they are all being lazy loaded immediately?

@MappedSuperclass
public abstract class PimaOrderComponentORM implements Serializable, EntityWithPK<Long> {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns(@JoinColumn(name="PIMA_ORDER_ID", referencedColumnName="ID"))
    private PimaOrderBE pimaOrder;
}

@MappedSuperclass
public abstract class PimaOrderORM implements Serializable, EntityWithPK<Long> {
    @Id
    @GeneratedValue(generator="SQ_PIMA_ORDER")
    @SequenceGenerator(name="SQ_PIMA_ORDER", sequenceName="SQ_PIMA_ORDER", allocationSize=20)
    @Column(name = "ID")
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns(@JoinColumn(name="VEHICLE_ID", referencedColumnName="ID"))
    private VehicleBE vehicle;
}

Each class has a concrete implementation.

When executing this query using JPA

        String esql = "SELECT p FROM PimaOrderComponentBE p WHERE p.pimaOrder.vehicle.id in :vehicles";
        Query query = em
                .createQuery(esql)
                .setHint(QueryHints.JDBC_FETCH_SIZE, 1000)
                .setParameter("vehicles", ids);
        List<PimaOrderComponentBE> results = query.getResultList();

At this point 20-30 queries are all executed fetching all the lazy values? I have put breakpoints on all the necesary classes, and cannot figure it out.

Weaving is required for some lazy relationships such as OneToMany and ManyToOne, and enabling other performance enhancements such as fetch groups and change tracking, as described at https://www.eclipse.org/eclipselink/documentation/2.5/concepts/app_dev007.htm

Weaving generally automatic occurs within EE 7 containers, but will require extra steps to function outside of the container or in non-EE7 containers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM