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