Hibernate 不支持访问用户类型上的字段以实现投影标准?
我的课程:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能已经意识到,UserType 不是实体。了解为什么无法访问 UserType 中的属性的最佳方法是使用
URL
作为示例。您不会执行请求 URL.host 的查询,而是请求 URL 本身。这就是为什么 UserType 应该知道如何将字符串转换为对象,以及如何将对象转换为字符串。所以,你必须使用这样的东西:看看 此 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: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.