gpt4 book ai didi

java - JPQL 选择 DiscriminatorValue 列的值

转载 作者:行者123 更新时间:2023-12-02 09:12:17 24 4
gpt4 key购买 nike

我有一个看起来像这样的实体

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "status_type", length = 1, discriminatorType = DiscriminatorType.STRING)
@DiscriminatorOptions(force = true)
@Table(name = "EnrollmentStatus")
@AttributeOverride(name = AbstractGeneratedUUIDEntity.ID_NAME, column = @Column(name = EnrollmentStatus.ENROLLMENT_STATUS_ID_COLUMN, length = 36))
public abstract class EnrollmentStatus extends AbstractGeneratedUUIDEntity {
}

然后还有 EnrollmentStatus 的子类,如下所示:

@Entity
@DiscriminatorValue("E")
public class EnrollmentStatusEnrolled extends EnrollmentStatus {
}

我需要编写选择status_type的JPQL查询。我找到的唯一解决方案是:

SELECT new com.casenet.dao.enrollment.TimelineProgramEnrollmentQueryResult(prog.id, stat.date, TYPE(stat))
FROM ProgramEnrollment prog
INNER JOIN prog.statuses stat
WHERE prog.obsolete = 0
AND (stat.date >= :startDate and stat.date <= :endDate)
ORDER BY stat.date ASC

问题是 TYPE(stat) 没有选择值 E 作为我期望的字符串,而是选择带有一堆元数据的 Class 。我可以像这样在java中访问DiscriminatorValue,但这一点都不好。

public TimelineProgramEnrollmentQueryResult(String enrollmentId, LocalDate statusDate, Object status) {
this.enrollmentId = enrollmentId;
this.statusDate = statusDate;
Annotation[] declaredAnnotations = ((Class) status).getDeclaredAnnotations();
for (Annotation annoation : declaredAnnotations) {
if (annoation instanceof DiscriminatorValue) {
this.statusType = ((DiscriminatorValue) annoation).value();
}
}
}

是否有一种优雅的方式从 JPQL 查询中选择 DiscriminatorValue (不是作为 TYPE,而是作为一个值,在我的例子中它应该是 String)?

最佳答案

没有其他 JPA 方法。

但是您可以将 status_type 列映射为只读属性:

@Column(name = "status_type", insertable=false, updateable=false)
private String statusType;

然后在查询中使用此属性。

关于java - JPQL 选择 DiscriminatorValue 列的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59306028/

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