【问题标题】:Caused by: org.hibernate.QueryException: could not resolve property from inner enum class原因:org.hibernate.QueryException:无法解析内部枚举类的属性
【发布时间】:2023-03-17 02:55:01
【问题描述】:

拥有这个域类并在spring 3.2.4下使用与JPA集成的hibernate 3.2.6

@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)

【问题讨论】:

    标签: java hibernate jpa enums


    【解决方案1】:

    认为这些更改可能对您有用:

    @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); 
    

    【讨论】:

    • 您不需要添加@Enumerated(EnumType.STRING)。它只告诉数据库应该如何存储枚举。也就是说:默认存储是按序排列的,因此应注意仅在现有枚举之后添加新枚举。当存储为字符串时,只有名称的更改是有问题的,而顺序的更改则不是。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-25
      • 2014-02-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-11
      • 2017-09-30
      • 1970-01-01
      相关资源
      最近更新 更多