本文介绍了Hibernate不支持访问用于投影标准的usertype的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  public class User 
{
@Type(type =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。



我的查询:

 列出结果= session.createCriteria(User.class)
。 setprojection(Projections.projectionList()
.add(Projections.property(accountType.name),accountType)

.setResultTransformer(Transformer.aliasToBean(UserSummary.class))
.list()

我遇到的问题是我的查询失败,

  org.hibernate.QueryException:无法解析属性:accountType.name:com.huskyenergy.routecommander.domain.rtc。用户

哦,你不能.createAlias(accountType,at),因为accountType不是一个关联。



有什么想法?

解决方案

,UserType不是一个实体,要了解为什么你不能访问Use中的一个属性, rType将使用 URL 作为示例。您不会执行询问URL.host的查询,而是询问URL本身。这就是为什么UserType应该知道如何将字符串转换为对象,将对象转换为字符串等。所以,你必须使用这样的东西:

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

请看和从测试套件。

但我认为真正的问题是为什么你不使用Enum(和@Enumerated),而不是这个UserType。我认为Enum更适合,因为它内部是一个UserType,但它对于Hibernate来说是本地的。


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?

解决方案

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.

这篇关于Hibernate不支持访问用于投影标准的usertype的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-10 23:46