2012-03-26 7 views
5

मैं एक उपclass विशेषता खोजने के लिए एक क्वेरी लिखने की कोशिश कर रहा हूं जो कि अपने सुपरक्लास में नहीं है और सुपरक्लास की सभी वस्तुओं को वापस कर रहा है। वर्तमान में मुझे एक NullPointerException मिल रहा है जब मैं कोशिश करता हूं और व्यक्ति करता हूं .get ("specialAttribute")।एक जेपीए CriteriaQuery के साथ subclass विशेषता पर कैसे खोज करें?

@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") 
      ) 
    ); 

उत्तर

9

here प्रदान किए गए संकेत के बाद अपनी समस्या हल करें।

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") 
     ) 
); 
संबंधित मुद्दे