简体   繁体   中英

How do i get insanceof object in spring data query

I have an Item class

@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

@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

@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. Is there something todo with 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);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM