gpt4 book ai didi

java - Hibernate @Filter 枚举集合

转载 作者:搜寻专家 更新时间:2023-10-30 23:02:35 28 4
gpt4 key购买 nike

我需要弄清楚如何使用定义为枚举的参数列表来应用基于注释的过滤:

@Column(name = "target_status")
@Enumerated(EnumType.STRING)
@Type(type="PGEnumConverter", parameters = {
@Parameter(name = "enumClassName",
value = "com.company.model.campaign.CampaignTarget$Status")
})
private Status targetStatus;

所以我的 @FilterDef 看起来像这样:

    @FilterDef(name="filterCampaignTargetByStatuses",
defaultCondition="target_status in (:statuses)",
parameters = @ParamDef(name = "statuses", type = "string"))

当我启用过滤器时,它看起来像这样:

    session.enableFilter("filterCampaignTargetByStatuses").
setParameterList("statuses", statuses);

我从 hibernate 中得到的错误是:

 org.hibernate.HibernateException: Incorrect type for parameter [statuses]

数据在PostgreSQL中,类型定义:

CREATE TYPE statuscmp AS ENUM ('ACTIVE','INACTIVE','PAUSED','DRAFT','SCHEDULED','ENDED','ARCHIVED');

我见过很多关于如何针对单个枚举值进行条件查询和筛选的 SO 问题,但还没有关于筛选集合 枚举值的问题。有没有办法显式转换各个值?

最佳答案

一般情况下您不必“强制转换”值,实际上您只需要以存储的形式传递值。

如果我们假设您的字段仅被注释为 @Enumerated(EnumType.STRING),则该列将是一个普通的 varchar 字段。(将 java 类型映射到 postgres 枚举是另一个大话题。)

如果您现在想要将您的 Status 枚举实例列表与数据库中的相关字符串值进行比较,请将其作为字符串集合传递,换句话说,将其称为 toString() 方法,如果它是 java enum

例如这是你的枚举:

public enum EntityStatus {
A, B, C;
}

这是你的实体:

import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;

@Entity
@FilterDef(name = "byMultipleStates", defaultCondition = "status in (:states)", parameters = @ParamDef(name = "states", type = "string"))
@Filter(name = "byMultipleStates", condition = "status in (:states)")
public class StatusEntity {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@Enumerated(EnumType.STRING)
private EntityStatus status;

public long getId() {
return id;
}

public EntityStatus getStatus() {
return status;
}

public void setStatus(EntityStatus status) {
this.status = status;
}

}

这可能是您要过滤的代码:

public List<StatusEntity> filterByStates(final Set<EntityStatus> states) {
final Session hibernateSession = entityManager.unwrap(Session.class);
hibernateSession.enableFilter("byMultipleStates").setParameterList("states",
states.stream().map(state -> state.toString()).collect(Collectors.toList()));
final Query query = hibernateSession.createQuery("SELECT e FROM StatusEntity e");

return query.list();
}

或者Java 8之前的方式:

public List<StatusEntity> filterByStates(final Set<EntityStatus> states) {
final Set<String> statesAsString = new HashSet<>();
for (final EntityStatus state : states) {
statesAsString.add(state.toString());
}

final Session hibernateSession = entityManager.unwrap(Session.class);
hibernateSession.enableFilter("byMultipleStates").setParameterList("states", statesAsString);
final Query query = hibernateSession.createQuery("SELECT e FROM StatusEntity e");

return query.list();
}

所以只过滤一组值是可能的。

关于java - Hibernate @Filter 枚举集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31597727/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com