2011-01-07 53 views
3

我的課表:Hibernate不支持訪問用於投影標準的usertypes字段?

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不是關聯。

有什麼想法?

回答

2

正如您可能已經意識到的那樣,UserType不是實體。瞭解無法訪問UserType屬性的最佳方法是使用URL作爲示例。您不會執行詢問URL.host的查詢,而是詢問URL本身。這就是爲什麼UserType應該知道如何將字符串轉換爲對象,將對象轉換爲字符串等。所以,你將不得不使用這樣的事情:在this UserType examplethe test case for it從測試套件

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

看。

但我認爲真正的問題是爲什麼你不使用枚舉(和@Enumerated),而不是此UserType。我認爲Enum更適合,因爲它內部是一個UserType,但它對Hibernate來說是「本地的」。

+0

我想要一些行爲的智能枚舉。如果我可以訪問AccountType上的東西,那會很好。*嘆* – 2011-01-10 22:24:23