如何在JPA CriteriaBuilder中引用内部枚举

时间:2018-10-19 07:01:43

标签: java jpa criteria-api

我在一个类内有一个Enum,将其实体的值存储在数据库中,我想引用此列进行查询。这是实体:

public class PostBox{
    private Message message;

    @ManyToOne
    @JoinColumn(name = "MESSAGE_ID")
    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }
}

public class Message{

    private Answer answer;

    @Column(name = "ANSWER")
    @Enumerated(EnumType.STRING)
    public Answer getAnswer() {
        return answer;
    }

    @AllArgsConstructor
    public static enum Answer{
        MAYBE_YES,
        MAYBE_NO,
        MAYBE_I_DONT_KNOW

        @Getter
        private final String label;
    }
}

我想用简单的英语达到的目标:Select * from POSTBOX where MESSAGE IN (SELECT * from MESSAGE where ANSWER = MAYBE_NO)

这就是我来的

@Override
public final Specification<PostBox> byFilter(final PostBoxFilter filter) {
    return new Specification<PostBox>() {  
        @Override    
        public Predicate toPredicate(Root<PostBox> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
            return builder.equal(root.join(PostBox_.message).get(Message_.answer), filter.getAnswer());
        }
    };
}

但是它给出了error : no suitable method found for get(SingularAttribute<Message,Answer>)

我应该如何访问此变量?

0 个答案:

没有答案