简体   繁体   English

如何在Spring数据查询中获取insanceof对象

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

I have an Item class 我有一个Item

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

And this two next class is subclass of Item 而这两个下一个类是Item子类

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

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

I also have an Inventory class that have Item as field 我也有一个Inventory类,有Item作为字段

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

    @ManyToOne 
    private Item item;
}

My Question is how do i get the instance of item field. 我的问题是如何获取item字段的实例 Is there something todo with dtype ? dtype吗?

public interface InventoryDao extends JpaRepository<Inventory,Long> {

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

}

I need to do something like 我需要做类似的事情

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

I solved it by my self. 我自己解决了。

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