Hibernate aliasToBean不会将字符串转换为Enum

标签 hibernate enums hql

我在下面的查询中选择列列表中的一个常量,并且我期望aliasToBean将字符串转换为适当的枚举类型。

我已经将具有枚举属性的其他实体映射为字符串,并且没有任何问题。

    @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

最佳答案

这是我从一个SO答案中找到的解决方案

Properties params = new Properties();
        params.put("enumClass", "models.IOStatusEnum");
        params.put("type", "12"); /*type 12 instructs to use the String representation of enum value*/
        Type myEnumType = new TypeLocatorImpl(new TypeResolver()).custom(EnumType.class, params);
        SQLQuery q = sess.createSQLQuery(queryString).addScalar("status", myEnumType);;

关于Hibernate aliasToBean不会将字符串转换为Enum,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8250955/

相关文章:

java - hibernate :无法在类上找到适当的构造函数 - HQL

java - JPA - 使用 mappedBy 属性定义拥有实体的区别

hibernate - org.hibernate.MappingException : Unknown entity: pack1. Persoana

c# - 如何确定从 ushort 继承的枚举类型?

在 C 中选择枚举还是定义?

swift - 通过具有关联值的枚举属性过滤项目数组

hadoop - 如何计算 Hive 表中小于特定行的行数?

json - 在 Quarkus 的 REST Web 服务中返回 JSON 时出现 LazyInitializationException

hibernate - Grails-Hibernate XML-自定义列名

sql - HQL:在对象上使用 elements()