2015-07-18 35 views
0

我在Hibernate 4上有一個Criteria查詢,它使用像這樣的投影列表;在Hibernate投影屬性上應用函數

Criteria c = getSession().createCriteria(Question.class); 

    ProjectionList projectionList = Projections.projectionList(); 
    projectionList.add(Projections.property("id"), "id"); 
    projectionList.add(Projections.property("type"), "type"); 

問題是,是一個枚舉值,我有一個通用的枚舉映射到一個我想調用的資源串的輔助類。即:

resourceHelper.getEnumResource(QuestionType.xxxx); 

該投影是否可以調用我的幫助器方法?我能想到的唯一解決方案是將該方法添加到枚舉本身,但這會迫使我使用@AutoWire MessageSource的實例(我使用Spring)來枚舉Enum,我認爲這可能有點矯枉過正。

+0

你在這種情況下使用DTO嗎? – Amogh

+0

是的,先生。我正在使用DTO和Transformers.aliasToBean()來映射值。 – tggm

+0

你可以展示DTO類嗎? – Amogh

回答

1

那麼@Amogh在評論中說,這是我覺得最好和最正確的方式。你還沒有發佈你的DTO代碼,這就是爲什麼它很難以代碼格式顯示。

這樣做我認爲你在DTO類中擁有屬性questionType,根據預測存儲值爲type

加入將舉行EnumResource像一個屬性:

private EnumResource enumResource; 

//唯一吸氣劑

public EnumResource getEnumResource() 
{  
    return this.enumResource;  
} 

而且在questionType二傳手你已經有代碼略低於行代碼的設置this.questionType添加下面這行將設置enumResource的值:

this.enumResource = resourceHelper.getEnumResource(this.questionType); 
+0

爲什麼你檢查'this.enumResource!= null',如果它返回null。你可以說'return this.enumResource;'沒有任何條件,所以如果它是null,它將返回null,否則無論值如何。 – Amogh

+0

@Amogh哦,是的,你說得對。對不起,代碼錯誤。我將編輯。 – user3813463