我有这个实体
@Entity
@Table(name = "DOCUMENT")
public class Document {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID",nullable = false)
private Long id;
@Column(name = "NAME",nullable = false)
private String name;
@Column(name = "BROKER_ID",nullable = true)
private String brokerId;
@ManyToOne
@JoinColumn(name = "TYPE_ID",nullable = false)
private DocumentType type;
@Column(name = "UPLOAD_DATE",nullable = false)
private Date uploadDate;
@Column(name = "LANGUAGE",nullable = false)
@Enumerated(EnumType.STRING)
private Language language;
@ElementCollection
@CollectionTable(name = "DOCUMENT_VIEWED_BY",joinColumns = @JoinColumn(name = "DOCUMENT_ID",referencedColumnName = "ID"))
@Column(name = "VIEWED_BY",nullable = false)
private Set<String> viewedBy;
@Column(name = "DOCUMENT_REFERENCE",nullable = false)
private String documentReference;
//constructors getters setters
然后我试图通过 brokerId、类型和分页从数据库中获取所有文档。一切正常,唯一的问题是我想获取一个带有这样的 case 表达式的额外列
final BooleanExpression b = new CaseBuilder()
.when(document.viewedBy.contains(brokerId)).then(true).otherwise(false);
final JPAQuery<DocumentProjection> query = new JPAQuery<>(this.em);
query.select(Projections.constructor(DocumentProjection.class,document.brokerId,document.documentReference,document.name,document.uploadDate,document.type.id,document.language,b));
获取此查询
Hibernate:
select
document0_.BROKER_ID as col_0_0_,document0_.DOCUMENT_REFERENCE as col_1_0_,document0_.NAME as col_2_0_,document0_.UPLOAD_DATE as col_3_0_,document0_.TYPE_ID as col_4_0_,document0_.LANGUAGE as col_5_0_,case
when ? in (viewedby1_.VIEWED_BY) then ?
else 0
end as col_6_0_
from
DOCUMENT document0_ cross
join
DOCUMENT_VIEWED_BY viewedby1_
where
document0_.ID=viewedby1_.DOCUMENT_ID
and (
document0_.BROKER_ID=?
or document0_.BROKER_ID is null
)
and (
document0_.TYPE_ID in (
?,?,?
)
)
order by
document0_.UPLOAD_DATE desc limit ?
这很好,唯一的问题是在 case
子句中......我想要类似 when viewedby1_.VIEWED_BY = ? then
而不是 when ? in (viewedby1_.VIEWED_BY) then
的东西,所以我试图从 {{1} 更改行} 到 document.viewedBy.contains(brokerId)
因为这是我在代码其他部分的 document.viewedBy.any().eq(brokerId)
子句中这样做的方式,它为我提供了我想要的语法,但是当谈到 where
在 {{ 1}} 它给了我一个错误
case
谢谢。