Hibernate 不支持访问用户类型上的字段以实现投影标准?

发布于 2024-10-10 18:44:14 字数 987 浏览 7 评论 0原文

我的课程:

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

有什么想法吗?

My Classes:

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");
}

I also have a properly setup AccountTypeUserType.

My Query:

List results = session.createCriteria(User.class)
   .setProjection(Projections.projectionList()
      .add(Projections.property("accountType.name"), "accountType)
   )
   .setResultTransformer(Transformer.aliasToBean(UserSummary.class))
   .list()

The problem I run into is that my query fails with...

org.hibernate.QueryException: could not resolve property: accountType.name of: com.huskyenergy.routecommander.domain.rtc.User

Oh and you cannot .createAlias("accountType", "at") either because accountType is not an association.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝海似她心 2024-10-17 18:44:14

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

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

看看 此 UserType 示例测试套件中的测试用例

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

As you probably realized, an UserType is not an entity. The best way to see why you can't access a property in an UserType is to use URL as the example. You won't do a query asking for URL.host, but for URL itself. That's why an UserType should know how to transform a String into an Object, and an Object into String, for instance. So, you'll have to use something like this:

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

Look at this UserType example and the test case for it from the test suite.

But I think the real question is why you are not using an Enum (and @Enumerated), instead of this UserType. I think that an Enum is a better fit, as it's internally an UserType, but it's "native" to Hibernate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文