在Hibernate中创建别名以引用子类问题

时间:2013-02-18 15:24:54

标签: hibernate mapping subclass criteria joined-subclass

我正在尝试创建一些别名来构建一个条件,以便按许多子类的属性进行搜索。这是我的模特:

public abstract class Entity {

protected int id;
protected PartyBasicGroup partyBasicGroup;

}

public class Person extends Entity {

}

public class Organization extends Entity {

protected PartyBasicGroup signatoryBasicGroup;
protected String jobTitle;  
}

我正在尝试为Person和Organization创建一些别名,如下所示:

criteria = criteria.createAlias("entity.person", "person", JoinType.LEFT_OUTER_JOIN);
criteria = criteria.createAlias("entity.organization", "organization", JoinType.LEFT_OUTER_JOIN);

但是我收到了一个错误:

Couldn't resolve property person for Entity

有什么帮助来解决这个问题?我只想知道如何创建别名来引用子类以访问子类属性。

谢谢!

1 个答案:

答案 0 :(得分:0)

我不明白你为什么要在这里使用别名。以下应该足够了:

Criteria criteria = session.createCriteria(Organization.class);
criteria = criteria.add(Restrictions.eq("jobTitle", "XYZ"));
List<Organization> organizations = (List<Organization>) criteria.list();