簡體   English   中英

如何在Spring數據查詢中獲取insanceof對象

[英]How do i get insanceof object in spring data query

我有一個Item

@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Item {
    @Id 
    private Long id;
    private String name;
}

而這兩個下一個類是Item子類

@Getter
@Setter
@Entity
public class RawMaterial extends Item {
    private String supplier;
}

@Getter
@Setter
@Entity
public class Product extends Item {
    private BigDecimal salePrice;
}

我也有一個Inventory類,有Item作為字段

@Getter
@Setter
@Entity
public class Inventory {
    @Id 
    private Long id;

    @ManyToOne 
    private Item item;
}

我的問題是如何獲取item字段的實例 dtype嗎?

public interface InventoryDao extends JpaRepository<Inventory,Long> {

    @Query("FROM Inventory WHERE item instance of ?1")
    public List<Inventory> getInventoryByItem(Class klazz);

}

我需要做類似的事情

List<Inventory> list = getInventoryByItem(Product.class);

我自己解決了。

public interface InventoryDao extends JpaRepository<Inventory,Long> {

    @Query("FROM Inventory WHERE TYPE(item.class) = ?1")
    public List<Inventory> getInventoryByItem(Class<? extends Item> klazz);

}

暫無
暫無

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

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