我正在使用JPA规范来执行多表关联查询。但是数据是错误的。我想用 “很多”中的条件查询。这是我的代码
1。这是我的某些映射类的代码:
@Entity
@Table(..)
public class SkuLinesEntity {
@Id
@Column(name = "pk_id")
private long pkId;
@ManyToOne(cascade = CascadeType.PERSIST,targetEntity = ItemLinesEntity.class)
@JoinColumn(name="fk_item_id",referencedColumnName="pk_id")
@JsonIgnore
private ItemLinesEntity itemLinesEntity;
...
}
@Entity
@Table(..)
public class OrdersEntity {
@Id
@Column(name = "pk_id")
private Long pkId;
@ManyToOne(cascade={CascadeType.REFRESH},targetEntity = ShopCartsEntity.class,optional = false)
@JoinColumn(name = "fk_cart_id")
@JsonIgnore
private ShopCartsEntity shopCartsEntity;
@OneToMany(mappedBy = "ordersEntity",fetch=FetchType.EAGER,targetEntity = ItemLinesEntity.class)
private List<ItemLinesEntity> items;
...
}
@Entity
@Table(...)
public class ItemLinesEntity {
@Id
@Column(name = "pk_id")
private Long pkId;
@ManyToOne(cascade = CascadeType.PERSIST,targetEntity = OrdersEntity.class)
@JoinColumn(name="fk_order_id",referencedColumnName="pk_id")
@JsonIgnore
private OrdersEntity ordersEntity;
@OneToMany(mappedBy = "itemLinesEntity",fetch=FetchType.EAGER,targetEntity = SkuLinesEntity.class)
private List<SkuLinesEntity> skus;
...
}
@Entity
@Table(...)
public class SkuLinesEntity {
@Id
@Column(name = "pk_id")
private Long pkId;
@ManyToOne(cascade = CascadeType.PERSIST,targetEntity = ItemLinesEntity.class)
@JoinColumn(name="fk_item_id",referencedColumnName="pk_id")
@JsonIgnore
private ItemLinesEntity itemLinesEntity;
...
}
``` java
**//This is my query code:**
Specification<ShopCartsEntity> find = new Specification<ShopCartsEntity>() {
@Override
public Predicate toPredicate(Root<ShopCartsEntity> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder cb) {
// Predicate p = cb.conjunction();
List<Predicate> predicates = new ArrayList<>();
Join<ShopCartsEntity, OrdersEntity> orders = root.join("orders");
if(!StringUtils.isBlank(shopOrder.getSoNumber())){
predicates
.add(cb.like(cb.upper(orders.get("epSoNumber")),"%"+ shopOrder.getSoNumber().toUpperCase()+"%"));
}
Join<OrdersEntity, ItemLinesEntity> items = orders.join("items");
if(!StringUtils.isBlank(shopOrder.getProductId())){
predicates
.add(cb.like(cb.upper(items.get("productId")),"%"+ shopOrder.getProductId().toUpperCase()+"%"));
}
if(!StringUtils.isBlank(shopOrder.getStyleNo())){
predicates
.add(cb.like(cb.upper(items.get("styleNo")),"%"+ shopOrder.getStyleNo().toUpperCase()+"%"));
}
predicates
.add(cb.equal(orders.get("dataFlag"),0));
return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
}
};
```
我正在寻找这个。但是生成的语句有多个 段落,不是我想要的。 只有父表才是正确的查询条件,以下所有都是基于ID的相关查询。 以下是结果比较
Generated SQL:
**Hibernate: select shopcartse0_.pk_id as pk_id1_3_, shopcartse0_.creation_time as creation2_3_, shopcartse0_.main_program_code as main_pro3_3_, shopcartse0_.sub_program_code as sub_prog4_3_, shopcartse0_.update_time as update_t5_3_, shopcartse0_.user_id as user_id6_3_ from public.ep2_sc_shop_carts shopcartse0_ inner join public.ep2_sc_orders orders1_ on shopcartse0_.pk_id=orders1_.fk_cart_id inner join public.ep2_sc_item_lines items2_ on orders1_.pk_id=items2_.fk_order_id where upper(shopcartse0_.user_id)=? and upper(shopcartse0_.main_program_code)=? and (upper(orders1_.ep_so_number) like ?) and orders1_.data_flag=0 limit ?
Hibernate: select .. from public.ep2_sc_orders orders0_ where orders0_.fk_cart_id=?
Hibernate: select .. from public.ep2_sc_item_lines items0_ where items0_.fk_order_id=?
Hibernate: select .. from public.ep2_sc_sku_lines skus0_ where skus0_.fk_item_id=?**
**//I hope:
// "records": {
// "orders": [{
// "soNumber": "EP2TGDY00000358",
// "orderId": 781,],
// "items": [{
// "itemId": 883,
// "itemType": "variable",
// "prodCode": "TGTGHKH002",
// "skus": []
// }
// ]
// }
// ]
// }
////But the display is indeed like this:
//
// "records": {
// "orders": [{
// "soNumber": "EP2TGDY00000358",
// "orderId": 781,],
// "items": [{
// "itemId": 883,
// "itemType": "variable",
// "prodCode": "TGTGHKH002",
// "skus": []
// },
// {
// "itemId": 884,
// "itemType": "variable",
// "prodId": "TGTGHKH003",
// "skus": []
// }
// ]
// }
// ]
// }****