原因:java.sql.SQLSyntaxErrorException:ORA-01722:JPA HIBERNATE HQL 中的数字无效

Caused by: java.sql.SQLSyntaxErrorException: ORA-01722: invalid number in JPA HIBERNATE HQL

有了这个class

    @Entity
    public class PriorityAreaKeyword {

        public enum PriorityAreaKey {

            ALL     ("ALL",    "ALL DEVICES"),
            IOS     ("IOS",    "IOS"),
            ANDROID ("ANDROID","ANDROID");

            private final String name;

            private final String id;

            private PriorityAreaKey(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")
        @Enumerated(EnumType.STRING)
        private PriorityAreaKey key;


        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public PriorityAreaKey getKey() {
            return key;
        }

        public void setKey(PriorityAreaKey key) {
            this.key = key;
        }

        public List<PriorityArea> getPriorityAreas() {
            return priorityAreas;
        }

        public void setPriorityAreas(List<PriorityArea> priorityAreas) {
            this.priorityAreas = priorityAreas;
        }
}

我在 DAO 中有这个工作正常的方法:

@Override
    @SuppressWarnings("unchecked")
    public Set<PriorityArea> findPriorityAreas(PriorityAreaKey key) {

        String jpql = "from PriorityAreaKeyword as pak where pak.key = :key";

        Query query = entityManager.createQuery(jpql);
        query.setParameter("key", key);     
        List<PriorityArea> priorityAreas =  query.getResultList();
        return new HashSet<PriorityArea>(priorityAreas);
    }

我创建了这样的视图v_report_beneficiary_list(id、电子邮件、priority_area_key)

/**
 *
 */
@Entity
@Table(name = "v_report_beneficiary_list")
public class ReportBeneficiaryItem {

    private Long id;
    private String email;
    private PriorityAreaKey priorityAreaKey;


    /**
     * @return the id
     */
    @Id
    public Long getId() {
        return id;
    }


    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }



    @Column(name = "email")
    public String getEmail() {
        return email;
    }


    public void setEmail(String email) {
        this.email = email;
    }


    @Column(name = "priority_area_key")
    public PriorityAreaKey getPriorityAreaKey() {
        return priorityAreaKey;
    }


    public void setPriorityAreaKey(PriorityAreaKey priorityAreaKey) {
        this.priorityAreaKey = priorityAreaKey;
    }

在 DAO 中,我创建了另一个这样的方法:

@苏

ppressWarnings("unchecked")
    @Override
    public List<ReportBeneficiaryItem> findReportProposalXBeneficiary(ProposalExportFilter filter) {

        // Create basic query
        String jpql = "from " + ReportBeneficiaryItem.class.getName() + " b where b.priorityAreaKey = :key ";

        // Create and execute jpa query
        Query query = createQuery(jpql);

        query.setParameter("key",      filter.getPriorityAreaKey());

        return query.getResultList();
    }

这给我抛出一个异常引起的异常:java.sql.SQLSyntaxErrorException:ORA-01722:无效数字

您在 ReportBeneficiaryItem#getPriorityAreaKey() 上缺少 @Enumerated(EnumType.STRING),就像在 PriorityAreaKeyword#key 上一样,因此它期望数据库中该字段的数字(枚举索引),但找到字符串

@Column(name = "priority_area_key")
@Enumerated(EnumType.STRING)
public PriorityAreaKey getPriorityAreaKey() {
    return priorityAreaKey;
}