3

我的课程:

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 不是一个关联。

有什么想法吗?

4

1 回答 1

2

您可能已经意识到,UserType 不是实体。了解为什么不能访问 UserType 中的属性的最佳方法是使用URL示例。您不会查询 URL.host,而是查询 URL 本身。这就是为什么 UserType 应该知道如何将 String 转换为 Object,并将 Object 转换为 String,例如。所以,你必须使用这样的东西:

.add(Projections.property("accountType.name"), AccountType.User)

查看这个 UserType 示例以及来自测试套件的测试用例

但我认为真正的问题是为什么你不使用枚举(和@Enumerated),而不是这个UserType。我认为 Enum 更合适,因为它在内部是 UserType,但它是 Hibernate 的“原生”。

于 2011-01-08T11:15:16.243 回答