我正在努力使用 Hibernate Criteria API。
在课堂Conversation
上我有:
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinTable(name = "Conversation_ConversationParticipants",
joinColumns = @JoinColumn(name = "ConversationID"),
inverseJoinColumns = @JoinColumn(name = "ConversationParticipantID"))
private List<ConversationParticipant> participants;
在课堂上ConversationParticipant
我有:
@OneToOne
@JoinColumn(name = "ParticipantID")
private User participant;
@Type(type = "true_false")
@Column(name = "Viewed")
private boolean viewed;
在我的标准中,我有
Criteria criteria = super.createCriteria();
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
criteria.createAlias("participants", "participants");
criteria.createAlias("participants.participant", "participant");
criteria.add(Restrictions.eq("participant.id", user.getId()));
return (List<Conversation>) criteria.list();
这一切正常,我可以看到给定用户的所有对话。但是,我想要查看用户尚未查看的所有对话。为此,我尝试添加:
criteria.add(Restrictions.eq("participants.viewed", false));
但是,这会返回 0 个对话(如果我输入“true”,我会得到相同的结果,并且我已经检查了 db 并确保存在同时具有 true 和 false 的值)。我怎样才能做到这一点?我究竟做错了什么?
运行的查询是:
from
Conversations this_
inner join
Conversation_ConversationParticipants participan4_
on this_.id=participan4_.ConversationID
inner join
ConversationParticipants participan1_
on participan4_.ConversationParticipantID=participan1_.id
inner join
Users participan2_
on participan1_.ParticipantID=participan2_.id
where
participan1_.Viewed=?
and participan2_.id=?
数据库表:
CREATE TABLE ConversationParticipants(ID BIGINT NOT NULL IDENTITY, Viewed CHAR(1), Type VARCHAR (255), ParticipantID BIGINT, PRIMARY KEY (ID));
根据下面的答案,我能够通过使用使其工作:
criteria.add(Restrictions.eq("participants.viewed", Boolean.FALSE));