我的课程:
public class User
{
@Type(type="AccountType")
private AccountType accountType;
}
public class AccountType
{
private String name;
private AccountType(String name)
{
this.name = name;
}
public static AccountType User = new AccountType("User");
public static AccountType Administrator = new AccountType("Administrator");
}
我也有正确设置的AccountTypeUserType。
我的查询:
List results = session.createCriteria(User.class)
.setProjection(Projections.projectionList()
.add(Projections.property("accountType.name"), "accountType)
)
.setResultTransformer(Transformer.aliasToBean(UserSummary.class))
.list()
我遇到的问题是我的查询失败了......
org.hibernate.QueryException: could not resolve property: accountType.name of: com.huskyenergy.routecommander.domain.rtc.User
哦,你不能.createAlias(“accountType”,“at”)因为accountType不是一个关联。
有什么想法吗?
答案 0 :(得分:2)
您可能已经意识到,UserType不是实体。了解无法访问UserType中的属性的最佳方法是使用URL
作为示例。您不会查询请求URL.host,而是查询URL本身。这就是为什么UserType应该知道如何将String转换为Object,以及将Object转换为String。所以,你必须使用这样的东西:
.add(Projections.property("accountType.name"), AccountType.User)
从测试套件中查看this UserType example和the test case for it。
但我认为真正的问题是为什么你没有使用Enum(和@Enumerated)而不是这个UserType。我认为Enum更适合,因为它内部是UserType,但它是Hibernate的“原生”。