亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JPQL - 使用過濾器連接查詢

JPQL - 使用過濾器連接查詢

米脂 2022-07-14 16:31:39
我有實體:第一的@Entity@Getter@Setter@NoArgsConstructorpublic class Technic implements Serializable {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Long id;    private String name;    private String gosNumber;    private String invNumber;    private String shassisNumber;    private String engineNumber;    @Column(length = 100)    private String yearOfMake;    @ManyToOne    private Farm farm;    @JsonManagedReference    @ManyToOne    private TechGroup techGroup;    @JsonManagedReference    @ManyToOne    private TechType techType;    @JsonManagedReference    @ManyToOne    private TechMark techMark;    @JsonIgnore    @CreationTimestamp    @Column(name = "creation_date", updatable = false)    private LocalDateTime createdDate;    @JsonIgnore    @Column(name = "updated_date")    @UpdateTimestamp    private LocalDateTime updatedDate;    @JsonIgnore    @Column(columnDefinition = "Bool default false")    private Boolean isDel;    @JsonManagedReference    @OneToMany(mappedBy = "technic")    private List<TechnicStatus> technicStatusList = new ArrayList<>();    public List<TechnicStatus> getTechnicStatusList() {        return technicStatusList;    }    public void setTechnicStatus(TechnicStatus technicStatus) {        this.technicStatusList = new ArrayList<>();        this.technicStatusList.add(technicStatus);    }第二:@Entity@Getter@Setter@NoArgsConstructorpublic class TechnicStatus implements Serializable  {}我想從我的數據庫中獲取結果,其中包含每個對象技術中的列表,我有列表 technicStatusList = new ArrayList<>(),我希望在其中只有一個值為 isActive=true 的 TechnicStatus。為此,我正確的 JPQL 查詢:TypedQuery<Technic> query = em.createQuery("Select t  from Technic t join TechnicStatus ts on t.id = ts.technic.id where t.isDel=false and ts.isActive=true and t.farm.id=:farmId order by t.techGroup.name, t.techType.name, t.techMark.name", Technic.class);但是得到一個包含 TechnicStatus 的結果,它返回一個帶有真假的 TechnicStatus (TechnicStatus.isActive=true, TechnicStatus.isActive=false)。
查看完整描述

3 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

無法在查詢條件中過濾相關集合。您可以通過在 TechnicalStatus 上進行選擇來獲得它: select ts from TechnicStatus ts join Technic t where ...


我注意到的另一件事:


添加新狀態時,您正在覆蓋現有狀態列表:


public void setTechnicStatus(TechnicStatus technicStatus) {

     this.technicStatusList = new ArrayList<>();

     this.technicStatusList.add(technicStatus);

}

technicStatusList在字段聲明中初始化。技術中的添加方法:


public void addTechnicStatus(TechnicStatus technicStatus) {

    getTechnicStatusList().add(technicStatus);

    technicStatus.setTechnic(this);

}

我注意到的另一件事:


使用 join 時不要使用on t.id = ts.technic.id. JPA 將在您編寫時創建正確的本機 SQL:join TechnicStatus ts WHERE ...


查看完整回答
反對 回復 2022-07-14
?
萬千封印

TA貢獻1891條經驗 獲得超3個贊

使用 join fetch 應該可以解決這個問題,這將迫使查詢一次急切地運行,并且只帶回與 where 子句匹配的記錄。所以你的查詢是:

TypedQuery<Technic> query = em.createQuery("Select t from Technic t join fetch TechnicStatus ts where t.isDel=false and ts.isActive=true and t.farm.id=:farmId order by t.techGroup.name, t.techType.name, t.techMark.name", Technic.class);


查看完整回答
反對 回復 2022-07-14
?
繁華開滿天機

TA貢獻1816條經驗 獲得超4個贊

與技術關聯的技術狀態列表將始終是您的映射定義的完整列表。


基本上你有2個選擇。如果您只對狀態為 Active 的 TechnicalStatus 感興趣,那么您可以在關聯上使用不可移植的 Hibernate 特定@Where子句。


@JsonManagedReference

@OneToMany(mappedBy = "technic")

@Where("active = 1")

private List<TechnicStatus> technicStatusList = new ArrayList<>();

https://dzone.com/articles/hibernate-where-clause


否則,您所做的只是從查詢方法返回一個技術狀態列表,這不是您想要的,而是您所擁有的。


查看完整回答
反對 回復 2022-07-14
  • 3 回答
  • 0 關注
  • 110 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號