5
我正在嘗試編寫一個查詢來搜索不在其超類中的子類屬性,並返回超類的所有對象。目前,當我嘗試並執行person.get(「specialAttribute」)時,我得到一個NullPointerException。如何使用JPA CriteriaQuery在子類屬性上進行搜索?
@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")
)
);