You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用JBoss EAP7.1时,Entity Graph导致Arquillian依赖不满足问题

嘿,我明白你想把FetchType.EAGER换成EntityGraph来优化关联数据加载——这确实是个更灵活的性能优化方案,能避免EAGER带来的不必要的关联加载问题。先把你的Customer实体类代码按规范格式化好:

@Entity
@NamedQueries({
    @NamedQuery(name="Customer.FIND_ALL", query="SELECT DISTINCT c FROM Customer c"),
    @NamedQuery(name="Customer.DELETE_ALL", query="DELETE FROM Customer c")
})
@NamedEntityGraphs({
    @NamedEntityGraph(
        name="Customer.GRAPH_ALL",
        attributeNodes= {
            @NamedAttributeNode("addresses"),
            @NamedAttributeNode("communications")
        }
    )
})
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;

    // 主键及基础属性示例
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String fullName;
    // ... 其他业务属性

    // 关联关系定义(这里假设是一对多关联,可根据实际场景调整)
    @OneToMany(mappedBy = "customer")
    private List<Address> addresses;

    @OneToMany(mappedBy = "customer")
    private List<Communication> communications;

    // Getter、Setter、构造方法等
    // ...
}

接下来给你个实际使用这个EntityGraph的示例,在查询时指定加载关联数据:

// 注入EntityManager
@PersistenceContext
private EntityManager em;

public List<Customer> getAllCustomersWithAssociations() {
    // 获取定义好的EntityGraph
    EntityGraph<?> customerGraph = em.getEntityGraph("Customer.GRAPH_ALL");
    // 创建命名查询
    TypedQuery<Customer> query = em.createNamedQuery("Customer.FIND_ALL", Customer.class);
    // 设置加载图的Hint,指定用这个EntityGraph加载关联数据
    query.setHint("javax.persistence.loadgraph", customerGraph);
    return query.getResultList();
}

这样做的核心优势在于:只有当你明确指定使用这个EntityGraph时,才会加载关联的addresses和communications,对比FetchType.EAGER那种无论什么场景都强制加载关联数据的方式,灵活性和性能都提升了不少,能有效避免不必要的笛卡尔积和内存消耗。

内容的提问来源于stack exchange,提问作者Hans

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:03:03