![](/img/trans.png)
[英]How do i get a Spring data query to satisfy some relations in a projection (ie pass a Collection to a projections)?
[英]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.