2012-03-26 17 views
5

Üst sınıfında olmayan bir alt sınıf niteliğini aramak ve süper sınıfın tüm nesnelerini döndürmek için bir sorgu yazmaya çalışıyorum. Şu anda ben try.get ("specialAttribute") yapmaya çalıştığımda bir NullPointerException alıyorum.JPC Kriteri Sorgusu ile alt sınıf niteliğinde nasıl arama yapılır?

@Inheritance(strategy = InheritanceType.JOINED) 
@Entity 
public abstract class Person { 
    public String searchableAttribute; 
} 

@Entity 
@Table(name = "normal_person") 
public class NormalPerson extends Person { 

} 

@Entity 
@Table(name = "special_person") 
public class SpecialPerson extends Person { 
    @Column(nullable = false, unique = true) 
    public String specialAttribute; 
} 

// Controller method 

    Root<Person> person = query.from(Person.class); 
    query.where(
      builder.or(
        builder.like(person.<String>get("specialAttribute"), "foo"), 
        builder.like(person.<String>get("searchableAttribute"), "foo") 
      ) 
    ); 

cevap

9

here verilen bir ipucu sonrasında kendi sorunumu çözdüm.

Root<Person> person = query.from(Person.class); 

Subquery<SpecialPerson> subQuery = query.subquery(SpecialPerson.class); 
Root<SpecialPerson> specialPersonRoot = subQuery.from(SpecialPerson.class); 

subQuery.select(specialPersonRoot); 
subQuery.where(builder.like(specialPersonRoot.<String>get("specialAttribute"), "foo")); 

query.where(
     builder.or(
       builder.in(person).value(subQuery) 
       builder.like(person.<String>get("searchableAttribute"), "foo") 
     ) 
); 
İlgili konular