是否可以在QueryDSL查询中使用枚举方法 [英] Is it possible to use a Enum-Method in a QueryDSL query

查看:57
本文介绍了是否可以在QueryDSL查询中使用枚举方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含枚举字段的实体类型.我使用JPA 2.1 AttributeConverter将枚举实例/值映射到数据库列.Enum值包含我想在QueryDSL查询中使用的其他信息,就像该信息将存储在数据库中一样.

I have an entity type that contains an enum field. I use a JPA 2.1 AttributeConverter to map enum instances/values to a database column. The Enum values contain additional information I'd like to use in a QueryDSL query as if that information would be stored in the database.

示例:包含messageType的消息实体.类型是一个枚举值.枚举确定消息是否为全局消息.在数据库中,枚举表示为一个int值.信息是否全局是枚举的一部分.

Example: A message entity that contains a messageType. The type is a enum value. The enum determines if the message is global or not. In the database, the enum is represented as an int-value. The information if global or not is part of the enum.

有可能写这样的东西吗?

Is it possible to write something like this?

import static com.example.QMessage.message;

@Repository
public class MessageDao {

    @PersistenceContext
    protected EntityManager em;

    public List<Message> getGlobalMessages() {
        return new JPAQuery(em)
            .from(message)
            .where(message.messageType.global.isTrue())
            .list(message);
    }
}

查询应仅返回具有MessageTypes的消息,其中 global == true .但是生成的 QMessage 类不提供该功能. message.messageType 提供一个 EnumPath ,而无法调用该枚举的功能.

The query should only return the messages with MessageTypes where global==true. But the generated QMessageclass doesn't provide that functionality. message.messageTypegives an EnumPath without the possibility to call a function of the enum.

枚举

public enum MessageType {
    WELCOME(0, true),
    REMINDER(1),
    IMPORTANT(0, true);

    private final int id;
    private final boolean global;

    private MessageType(int id) {
        this(id, false);
    }

    private MessageType(int id, boolean global) {
        this.id = id;
        this.global = global;
    }

    public int getId() {
        return this.id;
    }

    public boolean isGlobal() {
        return this.global;
    }
}

实体

@Entity
public class Message {

    @Id
    private long messageId;

    @Column(name = "MESSAGE")
    private String message;

    @Column(name = "MESSAGE_TYPE")
    private MessageType messageType;

    // Getter and Setter
    // ... 
}

转换器

@Converter(autoApply = true)
public class MessageTypeConverter implements AttributeConverter<Integer, String> {

    @Override
    public Integer convertToDatabaseColumn(MessageType messageType) {
        return messageType.getId();
    }

    @Override
    public Locale convertToEntityAttribute(Integer dbValue) {
        for(MessageType type : values()) {
            if(type.getId() == dbValue) {
                return type;
            }
        }
        throw new IllegalArgumentException("Unknown id");
    }
}

推荐答案

枚举在JPA中被视为值/文字类型,因此没有干净的方法可以通过属性/方法引用来处理它.

Enums are treated as value/literal types in JPA, so there is no clean way to handle it through a property/method reference.

这篇关于是否可以在QueryDSL查询中使用枚举方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆