【发布时间】:2012-03-26 16:27:30
【问题描述】:
我正在尝试编写一个查询来搜索不在其超类中的子类属性并返回超类的所有对象。目前,当我尝试做 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")
)
);
【问题讨论】:
标签: hibernate jpa-2.0 criteria-api