2016-03-30 1 views
0

でinsanceofオブジェクトを取得します私はItemクラスはどのように私は春のデータクエリ

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

を持っており、この2次クラスはItem

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

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

のサブクラスである、私はまたItemを持ってInventoryクラスを持っていますフィールドとして

@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); 
+0

http://stackoverflow.com/questions/2093025/how-to-perform-a-non-polymorphic-hql-query-in-hibernate多分 –

+0

おかげで..私はそれをしようとします:) – Mirza

+0

ので、コンクリートクラスごとにレポを使用してみませんか? –

答えて

1

ような何かをする必要があります。

public interface InventoryDao extends JpaRepository<Inventory,Long> { 

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

} 
関連する問題