1

这是我在 SQL 查询字符串中所期望的:

SELECT     dbo.FRIEND.FriendId, dbo.FRIEND.MemberId, dbo.FRIEND.FriendMemberId, dbo.FRIEND.DateAdded
FROM         dbo.FRIEND INNER JOIN
                      dbo.MEMBER ON dbo.FRIEND.FriendMemberId = dbo.MEMBER.MemberId
WHERE     (dbo.MEMBER.Activate = 1)

这是我有关系的实体:实体朋友字段

@Id
@Column(name = "[FriendId]")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long              friendId;

@Column(name = "[MemberId]")
private Long              memberId;

@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "[FriendMemberId]")
private Member            friendMember;

@Column(name = "[DateAdded]")
private Date              dateAdded;

实体成员字段

@Id
@Column(name = "[MemberId]")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long              memberId;

@Column(name = "[Activate]", columnDefinition = "boolean default true")
private boolean           activate;

这是我的 HQL 查询:

FROM Friend as f Left join f.Member as m WHERE f.MemberId = :memberId AND m.activate = true

但我得到了错误。那么我应该如何编写 HQL 查询获取数据及其条件取决于 member.activate ?

4

2 回答 2

2

您可以使用点符号访问子表,Hibernate 会为您处理连接。

SELECT FROM Friend f WHERE f.friendMember.activate = true

这就是 HQL 如此出色的部分原因。

于 2012-06-14T22:50:33.930 回答
1

您的查询有一些错字。访问字段时应使用字段名称而不是类型。

尝试这个:

FROM Friend as f Left join f.friendMember as m WHERE f.memberId = :memberId AND m.activate = true

如果你有其他问题。请给我看日志。

于 2012-04-12T02:52:11.560 回答