spring数据jpa @Query,枚举类型不返回数据

时间:2014-02-26 12:35:54

标签: java mysql spring hibernate jpa

到此为止。日志显示没有返回记录。

  

14:10:01.331 TRACE JdbcCoordinatorImpl - 注册语句[SELECT inspector。* FROM inspector inspector WHERE inspector.mailState IN(?,?)]   14:10:01.331 TRACE JdbcCoordinatorImpl - 注册最后一个查询语句[SELECT inspector。* FROM inspector inspector WHERE inspector.mailState IN(?,?)]   14:10:01.331 DEBUG Loader - bindNamedParameters()UNREAD - > stateList_0_ [1]   14:10:01.332 TRACE BasicBinder - 绑定参数[1]为[VARBINARY] - [UNREAD]   14:10:01.336 TRACE SerializationHelper - 开始对象的序列化[UNREAD]   14:10:01.336 DEBUG Loader - bindNamedParameters()BLOCK_FAILURE - > stateList_1_ [2]   14:10:01.337 TRACE BasicBinder - 绑定参数[2]为[VARBINARY] - [BLOCK_FAILURE]   14:10:01.337 TRACE SerializationHelper - 开始对象的序列化[BLOCK_FAILURE]   14:10:01.337 TRACE Loader - Bound [3]参数total   14:10:01.338 TRACE JdbcCoordinatorImpl - 注册结果集[com.mysql.jdbc.JDBC4ResultSet@41f0527f]   14:10:01.338 TRACE Loader - 处理结果集   14:10:01.338 TRACE Loader - 完成处理结果集(0行)   14:10:01.338 TRACE装载机 - 水合物总量:0

表肯定在mailState列中有'UNREAD'的记录。使用spring数据jpa的JpaRepository具有以下代码

@Query(value = "SELECT inspector.* FROM inspector inspector WHERE inspector.mailState IN :stateList", nativeQuery = true)
List<Inspector> findAllByState(@Param("stateList") List<MailState> stateList);

My Inspector类就是这样

@Entity
@Table(name = "inspector")
public class Inspector {
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long id;

    @Enumerated(EnumType.STRING)
    @Column(name = "mailState")
    private MailState state;

    //...getters and setters etc...

    public enum MailState {
        UNREAD("Unread"),
        BLOCK_SUCCESS("Block Success"),
        BLOCK_FAILURE("Block Failure");

        private final String value;

        MailState(String v) {
            value = v;
        }

        public String value() {
            return value;
        }

        public static MailState fromValue(String v) {
            for (MailState c: MailState.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }
    }
}

为什么当我能够清楚地看到记录并且hibernate加载查询的枚举时,它不返回任何记录。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您正在使用本机查询而不是JPQL查询,该查询将使用实体中定义的映射来使用适当的类型并正确绑定枚举。用JPQL查询替换您的查询:

@Query("select inspector FROM Inspector inspector where inspector.state in :stateList")