加入获取嵌套在@OneToMany 中的@ManyToOne
Join fetching @ManyToOne nested inside @OneToMany
我创建了以下实体来管理持久购物车:
ShoppingCart.java:
@Entity
public class ShoppingCart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@PrivateOwned
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL)
@OrderBy("creationTimestamp")
private List<ShoppingCartItem> items;
public ShoppingCart() {}
// Getters and setters...
}
ShoppingCartItem.java:
@Entity
@IdClass(ShoppingCartItemId.class)
public class ShoppingCartItem {
@Id
@ManyToOne
private Item item;
@Id
@ManyToOne
private ShoppingCart cart;
private int quantity;
@Column(precision = 17, scale = 2)
private BigDecimal price;
@Temporal(TemporalType.TIMESTAMP)
private Date creationTimestamp;
protected ShoppingCartItem() {}
@PrePersist
protected void prePersist() {
creationTimestamp = new Date();
}
public ShoppingCartItem(ShoppingCart cart, Item item, int quantity) {
this.cart = cart;
this.item = item;
this.quantity = quantity;
this.price = item.getPrice();
}
// Getters and setters...
}
Item.java:
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Brand brand;
private String model;
private String variant;
private String description;
@Column(precision = 17, scale = 2)
private BigDecimal price;
private int availability;
protected Item() {}
// Constructors, getters and setters...
}
当我发出以下 JPQL 查询时:
SELECT c FROM ShoppingCart c JOIN FETCH c.items WHERE c.id = :id
我注意到同一个 ShoppingCart
中的所有 ShoppingCartItem
都按预期在单个查询中检索,但 @ManyToOne private Item item;
字段不在连接中,并且单独查询每个 ShoppingCartItem
被发出以获取该字段 当被访问时 。
使用 EclipseLink,有没有办法在 join/batch 获取 ShoppingCartItem
s 时也获取 Item
s 连接?如何更改查询 and/or 代码?
如果您使用的是 EclipseLink,则可以查看 @BatchFetch and @JoinFetch 注释。
虽然带有别名的 left join fetch
s 似乎被忽略了,但我发现这个查询提示可以完成这项工作:
Query query = entityManager.createQuery("SELECT c FROM ShoppingCart c WHERE c.id = :id");
query.setHint("eclipselink.left-join-fetch", "c.items.item.brand");
这可能比注释方法更好,因为它可以为每个查询指定。
更新
此提示的使用中断了 @OrderBy("creationTimestamp")
,因此 ShoppingCartItem
不再按插入顺序返回。这可能是由于 EclipseLink 中的一个错误,但我认为它并没有造成太大伤害,因为我实际上只需要在向用户显示购物车时订购商品,而不是,例如,当用户登录和商品时在匿名购物车必须转移到用户购物车。
我创建了以下实体来管理持久购物车:
ShoppingCart.java:
@Entity
public class ShoppingCart {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@PrivateOwned
@OneToMany(mappedBy = "cart", cascade = CascadeType.ALL)
@OrderBy("creationTimestamp")
private List<ShoppingCartItem> items;
public ShoppingCart() {}
// Getters and setters...
}
ShoppingCartItem.java:
@Entity
@IdClass(ShoppingCartItemId.class)
public class ShoppingCartItem {
@Id
@ManyToOne
private Item item;
@Id
@ManyToOne
private ShoppingCart cart;
private int quantity;
@Column(precision = 17, scale = 2)
private BigDecimal price;
@Temporal(TemporalType.TIMESTAMP)
private Date creationTimestamp;
protected ShoppingCartItem() {}
@PrePersist
protected void prePersist() {
creationTimestamp = new Date();
}
public ShoppingCartItem(ShoppingCart cart, Item item, int quantity) {
this.cart = cart;
this.item = item;
this.quantity = quantity;
this.price = item.getPrice();
}
// Getters and setters...
}
Item.java:
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Brand brand;
private String model;
private String variant;
private String description;
@Column(precision = 17, scale = 2)
private BigDecimal price;
private int availability;
protected Item() {}
// Constructors, getters and setters...
}
当我发出以下 JPQL 查询时:
SELECT c FROM ShoppingCart c JOIN FETCH c.items WHERE c.id = :id
我注意到同一个 ShoppingCart
中的所有 ShoppingCartItem
都按预期在单个查询中检索,但 @ManyToOne private Item item;
字段不在连接中,并且单独查询每个 ShoppingCartItem
被发出以获取该字段 当被访问时 。
使用 EclipseLink,有没有办法在 join/batch 获取 ShoppingCartItem
s 时也获取 Item
s 连接?如何更改查询 and/or 代码?
如果您使用的是 EclipseLink,则可以查看 @BatchFetch and @JoinFetch 注释。
虽然带有别名的 left join fetch
s 似乎被忽略了,但我发现这个查询提示可以完成这项工作:
Query query = entityManager.createQuery("SELECT c FROM ShoppingCart c WHERE c.id = :id");
query.setHint("eclipselink.left-join-fetch", "c.items.item.brand");
这可能比注释方法更好,因为它可以为每个查询指定。
更新
此提示的使用中断了 @OrderBy("creationTimestamp")
,因此 ShoppingCartItem
不再按插入顺序返回。这可能是由于 EclipseLink 中的一个错误,但我认为它并没有造成太大伤害,因为我实际上只需要在向用户显示购物车时订购商品,而不是,例如,当用户登录和商品时在匿名购物车必须转移到用户购物车。