JPA错误:状态字段路径“***”无法解析为有效类型

时间:2017-03-31 03:47:02

标签: jpa spring-data jpql

After running the following query method....   


@Query(value = "select s.owner, " +
            "count(case when s.status = 3 then 1 else null end) as agreed," +
            "count(case when s.status = 2 then 1 else null end) as rejected," +
            "count(case when s.status = 1 then 1 else null end) as pending " +
            "from Schedule s where (s.owner = ?1) " +
            "group by s.owner")

这里状态是从枚举获取的,我有一个转换器类。但是在运行时它给出了以下异常,即在转换器类com.orsbv.hcs.model.schedule.ScheduleStatus $ TypeConverter上调用convertToDatabaseColumn值为1 .E

关于这个问题的任何帮助都会非常有帮助。请专家注意。

这是错误日志:

ReportQuery(referenceClass=Schedule jpql="select s.owner, count(case when s.status = 1 then 1 else null end) as agreed,count(case when s.status = 2 then 1 else null end) as rejected,count(case when s.status = 3 then 1 else null end) as pending from Schedule s where (s.owner = ?1) group by s.owner")
        at org.eclipse.persistence.exceptions.QueryException.prepareFailed(QueryException.java:1596)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:685)
        at org.eclipse.persistence.queries.ObjectLevelReadQuery.checkPrepare(ObjectLevelReadQuery.java:911)
        at org.eclipse.persistence.queries.DatabaseQuery.checkPrepare(DatabaseQuery.java:615)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:194)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.buildEJBQLDatabaseQuery(EJBQueryImpl.java:116)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:102)
        at org.eclipse.persistence.internal.jpa.EJBQueryImpl.<init>(EJBQueryImpl.java:86)
        at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createQuery(EntityManagerImpl.java:1614)
        at sun.reflect.GeneratedMethodAccessor86.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:344)
        at com.sun.proxy.$Proxy75.createQuery(Unknown Source)
        at org.springframework.data.jpa.repository.query.SimpleJpaQuery.validateQuery(SimpleJpaQuery.java:86)
        ... 48 more
Caused by: javax.persistence.PersistenceException: An exception occurred while calling convertToDatabaseColumn on converter class com.orsbv.hcs.model.schedule.ScheduleStatus$TypeConverter with value 1

2 个答案:

答案 0 :(得分:1)

你应该做任何一件事

更改s.status = 3(这应该是枚举) 示例s.status = Status.ACTIVE

更改s.status(应为整数)= 3 示例:s.status.id = 3

答案 1 :(得分:0)

使用完全合格的软件包名称可以解决此问题。

@Query(value = "select s.owner, " + "count(case when s.status = com.orsbv.hcs.model.schedule.ScheduleStatus.AGREED then 1 else null end) as agreed," + "count(case when s.status = com.orsbv.hcs.model.schedule.ScheduleStatus.REJECTED then 1 else null end) as rejected," + "count(case when s.status = com.orsbv.hcs.model.schedule.ScheduleStatus.PENDING then 1 else null end) as pending " + "from Schedule s where (s.owner = ?1) " + "group by s.owner")