我正在使用 spring ex 探索 JPA 2.1 的新功能。EntityGraph 功能通过使用产品、采购订单和订单项目之间的示例关系进行示例 CRUD 操作。
下面是我为主 bean 编写的代码,我定义了一个命名查询来检索所有数据,以及实体图
@Entity
@Table(name = "purchase_order")
@NamedQueries({
@NamedQuery(name = "Order.findAll", query = "SELECT o FROM Order o")})
@NamedEntityGraph(name = "graph.Order.items", attributeNodes = @NamedAttributeNode(value = "items", subgraph = "items"),
subgraphs = @NamedSubgraph(name = "items", attributeNodes = @NamedAttributeNode("product")))
public class Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id = null;
@Version
@Column(name = "version")
private int version = 0;
@Column
private String orderNumber;
@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
private Set<OrderItem> items = new HashSet<OrderItem>();
... getter and setter methods
我正在尝试从我拥有的 DAO 方法调用命名查询和图形,如下所示
public List<Order> getOrderDetails() {
return (List<Order>) entityManager.createNamedQuery("Order.findAll").setHint("javax.persistence.loadgraph",
entityManager.getEntityGraph("graph.Order.items")).getResultList();
}
尽管我尝试更改“javax.persistence.fetchgraph”和“javax.persistence.loadgraph”之间的提示,但调用 DAO 方法的结果返回零索引,请咨询。