如何在 spring 数据查询中获取 instanceof 对象
How do i get insanceof object in spring data query
我有一个Item
class
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Item {
@Id
private Long id;
private String name;
}
而这两个 class 是 Item
的子class
@Getter
@Setter
@Entity
public class RawMaterial extends Item {
private String supplier;
}
@Getter
@Setter
@Entity
public class Product extends Item {
private BigDecimal salePrice;
}
我还有一个 Inventory
class 字段 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);
}
我有一个Item
class
@Getter
@Setter
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Item {
@Id
private Long id;
private String name;
}
而这两个 class 是 Item
@Getter
@Setter
@Entity
public class RawMaterial extends Item {
private String supplier;
}
@Getter
@Setter
@Entity
public class Product extends Item {
private BigDecimal salePrice;
}
我还有一个 Inventory
class 字段 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);
}