2
我遇到了Criteria和HQL查詢行爲之間奇怪的不一致。簡化的域模型:抽象類的Hibernate查詢不一致
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table("TITLES")
public abstract class ParentGroup {}
// no JPA annotations
public abstract class ParentTitle extends ParentGroup {}
@Entity
@DiscriminatorValue("SUB_CHILD")
public class SubTitle extends ParentTitle {}
下面是顯示了奇怪的行爲測試:
@Test
public void testQueryByAbstractSuperClass() {
List<ParentTitle> list = session.createCriteria(ParentTitle.class).list();
assertEquals(1, list.size()); //passes
list = session.createQuery("from ParentTitle").list(); //exception thrown
assertEquals(1, list.size());
}
但下列情況除外:
org.hibernate.hql.ast.QuerySyntaxException: ParentTitle is not mapped [from ParentTitle]
爲什麼HQL查詢不工作?