1

我有一个与其他实体有OneToMany关系的实体,例如Boat下面的实体。实际上,实体有很多字段。

@Entity
public class Boat {
    @Id
    private long id;
    private String name;

    @OneToMany(fetch=FetchType.EAGER)
    private List<Gun> guns;

    public List<Gun> getGuns() {
        return guns;
    }

    public String getName() {
        return name;
    }
}

对于某些搜索,我只对基本属性感兴趣,Boat不想检索任何OneToMany关系。一种选择是与 建立所有OneToMany关系fetch=FetchType.LAZY

但是,延迟加载会产生两个问题:

  1. OneToMany延迟加载会围绕我不需要的关系创建一个代理。我根本不需要访问这种OneToMany关系我需要我能得到的每一个性能改进。
  2. 许多其他查询需要预先获取,并且将所有OneToManyManyToOne关系更改为延迟获取会使其他查询变得麻烦。

有没有办法在不使用延迟获取的情况下选择应该动态检索哪些关系。这可能使用标准 JPQL 吗?

我在 Glassfish 中使用 EclipseLink。

我以前调查过 EclipseLink 提取组,这可以解决我的问题。但是,我遇到了这个问题:https ://stackoverflow.com/questions/19577864/classdescriptor-returns-null-fetchgroupmanager-when-trying-to-create-an-eclipsel

4

1 回答 1

1

Don't return a boat entity if all you want is the name or other basic attributes. JPA allows returning raw data rather than your manged entities if tht is what you are after. Using lazy on a relationship that isn't always required though will speed things up, not slow it down, but it does require planning when serialization might be involved and the relation is needed. Eclipselink also provides fetch and attribute groups to partially load entities as needed. See http://wiki.eclipse.org/EclipseLink/Examples/JPA/AttributeGroup for details

于 2013-10-27T16:12:11.353 回答