我正在尝试编写一个查询来搜索不在其超类中的子类属性,并返回超类的所有对象。目前,当我尝试执行person.get(“specialAttribute”)时,我得到一个NullPointerException。
@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")
)
);
答案 0 :(得分: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")
)
);