私は列リストの定数を選択しているので、適切な列挙型に文字列を変換するためのaliasToBeanが必要です。Hibernate aliasToBeanは文字列をEnumに変換しません
私は文字列としてenumプロパティを持つ他のエンティティをマップしていますが、問題はありません。
@SuppressWarnings("unchecked")
List<AssociatedEntity> fileList = queryUtil.createHQLQuery((
"select '" + AssociatedEntityTypeEnum.ASSOCIATED_ENTITY_TYPE_FILE + "' as associatedEntityType," +
" a.file2Id as id," +
" f.name as name" +
" from File f, Association a" +
" where f.id = :fileId" +
" and a.file1Id = f.id" +
" and a.associationType = :associationType"
))
.setParameter("fileId", fileId)
.setParameter("associationType", AssociationTypeEnum.ASSOCIATION_TYPE_FILE_FILE)
.setResultTransformer(Transformers.aliasToBean(AssociatedEntity.class))
.list();
これはBeanです:
public class AssociatedEntity {
public AssociatedEntity() {}
@Enumerated(EnumType.STRING)
private AssociatedEntityTypeEnum associatedEntityType;
public AssociatedEntityTypeEnum getAssociatedEntityType() { return this.associatedEntityType; }
public void setAssociatedEntityType(AssociatedEntityTypeEnum associatedEntityType) { this.associatedEntityType = associatedEntityType; }
private Integer id;
public Integer getId() { return this.id; }
public void setId(Integer id) { this.id = id; }
private String name;
public void setName(String name) { this.name = name; }
public String getName() { return this.name; }
}
、ここではエラーは以下のとおりです。フィールドの上に列挙ハンドラを作成し、指定する方法
23.11.2011 17:05:25 INFO [http-8080-2] (QueryUtil:createHQLQuery) - select 'ASSOCIATED_ENTITY_TYPE_FILE' as associatedEntityType, a.file2Id as id, f.name as name from File f, Association a where f.id = :fileId and a.file1Id = f.id and a.associationType = :associationType
23.11.2011 17:05:25 ERROR [http-8080-2] (BasicPropertyAccessor$BasicSetter:set) - IllegalArgumentException in class: com.twoh.dto.AssociatedEntity, setter method of property: associatedEntityType
23.11.2011 17:05:25 ERROR [http-8080-2] (BasicPropertyAccessor$BasicSetter:set) - expected type: com.twoh.dto.enums.AssociatedEntityTypeEnum, actual value: java.lang.String