2016-03-30 9 views
0

Ich habe eine Item KlasseWie kann ich insanceof Objekt im Frühjahr Datenabfrage

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

Und diese zwei nächste Klasse erhalten, ist Unterklasse von Item

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

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

ich auch eine Inventory Klasse, die Item haben als Feld

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

    @ManyToOne 
    private Item item; 
} 

Meine Frage Wie bekomme ich die Instanz vonitem Feld. Gibt es etwas mit dtype?

public interface InventoryDao extends JpaRepository<Inventory,Long> { 

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

} 

Ich brauche wie etwas zu tun

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

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

+0

Dank .. ich werde es versuchen :) – Mirza

+0

So Warum nicht ein Repo pro Betonklasse verwenden? –

Antwort

1

ich es von mir selbst gelöst.

public interface InventoryDao extends JpaRepository<Inventory,Long> { 

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

}