0

我正在使用 OpenJPA 2.2.1 并遇到以下问题:

TypedQuery<OdpObjectScheduleEntity> q = em.createQuery(
    "SELECT s FROM OdpObjectScheduleEntity s WHERE s.updateFreq IS NOT NULL",
    OdpObjectScheduleEntity.class);
List<OdpObjectScheduleEntity> result = q.getResultList();

这是OdpObjectScheduleEntity

package ru.focusmedia.odp.server.datastore.jpa.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;

import ru.focusmedia.odp.server.datastore.api.objects.OdpObjectScheduleRecord;

@Entity
@Table(name = "object_schedules")
public class OdpObjectScheduleEntity implements OdpObjectScheduleRecord {
    // fetch=FetchType.EAGER is the default. 
    // I've also tried explicitly setting it, no difference.
    @OneToOne(optional = false) 
    @Column(nullable = false)
    @Id
    private OdpObjectEntity object;

    @Column(name = "update_freq_seconds", nullable = true)
    private Integer updateFreq;

    protected OdpObjectScheduleEntity() {
        // for JPA
    }

    public OdpObjectScheduleEntity(OdpObjectEntity object, Integer updateFreq) {
        this.object = object;
        this.updateFreq = updateFreq;
    }

    @Override
    public OdpObjectEntity getObject() {
        return object;
    }

    @Override
    public Integer getUpdateFreq() {
        return updateFreq;
    }

    public void setUpdateFreq(Integer updateFreq) {
        this.updateFreq = updateFreq;
    }

    @Override
    public String toString() {
        return "OdpObjectScheduleEntity [object=" + object + ", updateFreq="
                + updateFreq + "]";
    }
}

但是 的元素result看起来好像对象是懒惰地获取的,例如OdpObjectScheduleEntity [object=OdpObjectEntity [id=23851, name=null, parent=null, odpClass=null], updateFreq=120]. 我是否遗漏了一些明显的东西,或者这是 OpenJPA 中的错误?我可以通过做一些可怕的事情来解决这个问题

for (OdpObjectScheduleEntity schedule : result) {
    // simply schedule.getObject() doesn't work
    schedule.getObject().toString();
}

有没有更好的方法来强制加载关系?

4

1 回答 1

1

我从来没有使用过 OpenJPA,所以我不能告诉你到底出了什么问题,但你可以尝试使用JOIN FETCH

TypedQuery<OdpObjectScheduleEntity> q = em.createQuery(
    "SELECT s FROM OdpObjectScheduleEntity s LEFT JOIN FETCH s.object WHERE s.updateFreq IS NOT NULL",
    OdpObjectScheduleEntity.class);
List<OdpObjectScheduleEntity> result = q.getResultList();

有关更多信息,请参见例如http://docs.oracle.com/html/E24396_01/ejb3_langref.html#ejb3_langref_fetch_joins

于 2013-02-03T09:32:26.257 回答