簡體   English   中英

LEFT JOIN FETCH 在運行記錄到數據庫客戶端控制台的查詢時返回空子列表返回所有必需的行

[英]LEFT JOIN FETCH returns empty child list while running the query logged to console in DB client returns all the required rows

我正試圖與所有孩子一起急切地獲取父母的數據。 我正在使用LEFT JOIN FETCH 對於一些孩子,數據被檢索,但有一些孩子返回空列表,盡管數據庫中有數據。 在一個用例中,對於其中一個孩子,我得到一個空列表,但數據庫中有一個孩子的記錄。

為了簡單起見,我從下面的代碼中刪除了其他孩子。

數據庫:DB2

父實體

@Entity
@Table(name = "INVENTORY_DATA", schema = "PREDICTIVE")
@Getter
@Setter
@NoArgsConstructor
public class InventoryDataEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "INVENTORY_DATA_ID", insertable = false, updatable = false)
private Integer inventoryDataId;

@Embedded
@NaturalId
private InventoryDataEntityPK inventoryDataNaturalKey;

 // Some other Fields

@OneToMany(mappedBy = "inventoryDataEntity", cascade = {CascadeType.ALL}, orphanRemoval = true)
private Set<DriveEntity> driveEntities;

    // equals and hashcode
}

有問題的子實體

@Entity
@Table(name = "DRIVE", schema = "COMP")
@Getter
@Setter
public class DriveEntity implements Serializable {

@EmbeddedId
private DriveEntityPK driveNaturalKey = new DriveEntityPK();

@Column(name = "DISKSERIAL")
private String diskSerial;

@Column(name = "FIRMWARE")
private String firmware;

// Other fields

@ManyToOne
@MapsId("inventoryDataId")
@JoinColumn(name = "INVENTORY_DATA_ID", referencedColumnName = "INVENTORY_DATA_ID")
protected InventoryDataEntity inventoryDataEntity;

@Override
public boolean equals(Object o) {
    if (this == o) {
        return true;
    }
    if (o == null || getClass() != o.getClass()) {
        return false;
    }
    DriveEntity that = (DriveEntity) o;
    return Objects.equals(driveNaturalKey, that.driveNaturalKey);
}

@Override
public int hashCode() {
    return Objects.hash(driveNaturalKey);
}
}

詢問:

@Override
public List<InventoryDataEntity> findPaginatedDataList(int dataPageSize, int offset) {

    var inventoryDataIds = entityManager.createQuery(JPQLQueries.INVENTORY_DATA_ID, Integer.class)
            .setFirstResult(offset)
            .setMaxResults(dataPageSize).getResultList();

  **The issue comes in below part**

    var inv =  entityManager.createQuery("select distinct invd from InventoryDataEntity invd LEFT JOIN FETCH invd.driveEntities de where invd.inventoryDataId in (:ids)", InventoryDataEntity.class).setParameter("ids", inventoryDataIds).getResultList();

    return inv;
}

查詢記錄到控制台:

select
distinct inventoryd0_.inventory_data_id as inventor1_13_0_,
inventoryd0_.machine_type as machin117_13_0_,
inventoryd0_.manufacturer as manufa118_13_0_,
inventoryd0_.model as model119_13_0_,
inventoryd0_.serial as serial120_13_0_,
driveentit1_.device_type as device_t1_5_1_,
driveentit1_.fru_pn as fru_pn2_5_1_,
driveentit1_.hardware_id as hardware3_5_1_,
driveentit1_.inventory_data_id as inventor4_5_1_,
driveentit1_.machine_type as machine_5_5_1_,
driveentit1_.model as model6_5_1_,
driveentit1_.ntap_pn as ntap_pn7_5_1_,
driveentit1_.part_number as part_num8_5_1_,
driveentit1_.product_id as product_9_5_1_,
driveentit1_.serial as serial10_5_1_,
driveentit1_.active_cluster as active_11_5_1_,
driveentit1_.brandcode_30 as brandco12_5_1_,
driveentit1_.critical_min_code_level as critica13_5_1_,
driveentit1_.critical_min_level as critica14_5_1_,
driveentit1_.disk_model as disk_mo15_5_1_,
driveentit1_.diskserial as diskser16_5_1_,
driveentit1_.drive_age_month as drive_a17_5_1_,
driveentit1_.drivename as drivena18_5_1_,
driveentit1_.firmware as firmwar19_5_1_,
driveentit1_.hb_status_code as hb_stat20_5_1_,
driveentit1_.heartbeatdate as heartbe21_5_1_,
driveentit1_.insert_ts as insert_22_5_1_,
driveentit1_.is_replaced as is_repl23_5_1_,
driveentit1_.is_replaced_ts as is_repl24_5_1_,
driveentit1_.king_cobra_installation_perc as king_co25_5_1_,
driveentit1_.min_code_level as min_cod26_5_1_,
driveentit1_.min_level as min_lev27_5_1_,
driveentit1_.right_diskserial as right_d28_5_1_,
driveentit1_.source_name as source_29_5_1_,
driveentit1_.target_code_level as target_30_5_1_,
driveentit1_.target_level as target_31_5_1_,
driveentit1_.update_ts as update_32_5_1_,
driveentit1_.inventory_data_id as inventor4_5_0__,
driveentit1_.device_type as device_t1_5_0__,
driveentit1_.fru_pn as fru_pn2_5_0__,
driveentit1_.hardware_id as hardware3_5_0__,
driveentit1_.machine_type as machine_5_5_0__,
driveentit1_.model as model6_5_0__,
driveentit1_.ntap_pn as ntap_pn7_5_0__,
driveentit1_.part_number as part_num8_5_0__,
driveentit1_.product_id as product_9_5_0__,
driveentit1_.serial as serial10_5_0__ 
from
   predictive.inventory_data inventoryd0_ 
left outer join
  comp.drive driveentit1_ 
    on inventoryd0_.inventory_data_id=driveentit1_.inventory_data_id 
where
  inventoryd0_.inventory_data_id in (
    ?
)
o.h.t.d.s.BasicBinder                    : binding parameter [1] as [INTEGER] - [223998]

在此處輸入圖像描述

在此處輸入圖像描述

數據庫客戶端中運行相同的查詢:

在此處輸入圖像描述

如果有人可以幫助我連接我在這里丟失的點。

更新:我們發現了問題。 問題是子實體的主鍵是復合的。 對於數據庫中的所有條目,其中一個字段是 null。 Hibernate 標記了一行 null,即使復合鍵的單個列是 null。數據團隊已經更改了復合鍵,現在對我來說一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM