导致:org.hibernate.QueryException:无法解析内部枚举类的属性



在 spring 3.2.4 下使用此域类并使用 hibernate 3.2.6 与 JPA 集成

@Entity
public class PriorityDeviceKeyword {
    public enum PriorityDeviceKey {
        ALL     ("ALL",    "ALL DEVICES"),
        IOS     ("IOS",    "IOS"),
        ANDROID ("ANDROID","ANDROID");
        private final String name;
        private final String id;
        private PriorityDeviceKey(String name, String id) {
            this.name = name;
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public String getId() {
            return id;
        }
    }
    @Id
    private Long id;
    @Column(name = "key")
    private PriorityDeviceKey key;

    @ManyToMany
    @JoinTable(name = "t_priority_device_set", joinColumns = @JoinColumn(name = "priority_device__id", referencedColumnName = "id"))
    private List<PriorityDevice> priorityDevices;
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public PriorityDeviceKey getKey() {
        return key;
    }
    public void setKey(PriorityDeviceKey key) {
        this.key = key;
    }
    public List<PriorityDevice> getPriorityDevices() {
        return priorityDevices;
    }
    public void setPriorityDevices(List<PriorityDevice> priorityDevices) {
        this.priorityDevices = priorityDevices;
    }
}

执行此查询时,我在执行的DAO类中有以下方法

@Override
    @SuppressWarnings("unchecked")
    public Set<PriorityDevices> findPriorityAreas(PriorityDevicesKey key) {
        String jpql = "from PriorityDevices as pak where pak.key.name = :keyName";
        Query query = entityManager.createQuery(jpql);
        query.setParameter("keyName", key.getName());       
        List<PriorityDevices> priorityDevices =  query.getResultList();
        return new HashSet<PriorityDevices>(priorityDevices);
    }

我收到应用程序抛出的异常:

2015-01-14 13:14:50,936 ERROR [com.controller.errors.Error500Controller] - Application thrown an exception
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not resolve property: name of: com.domain.PriorityDevicesKeyword [from com.domain.PriorityDevicesKeyword as
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:624)
        at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:96)
        at sun.reflect.GeneratedMethodAccessor440.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

认为这些更改可能适合您:

@Column(name = "key")
@Enumerated(EnumType.STRING)
private PriorityAreaKey key;

String jpql = "from PriorityAreaKeyword as pak where pak.key = :keyName";
Query query = entityManager.createQuery(jpql);
query.setParameter("keyName", key); 

Hibernate将枚举存储为原始枚举。或者,当字段用 @Enumerated(EnumType.STRING) 注释时,作为带有 Ennum 短名称的字符串。当注释的有效名称将被{ALL, IOS, ANDROID} .无论哪种方式,只有一个字段,枚举本身的属性不会被存储,毕竟它们是常量的。

如果要查询枚举值,则必须查询pak.key = :key并使用key作为参数。Hibernate将执行所需的序号或字符串转换。

最新更新