我们有以下域名: 成员和渠道。
会员 - 相当于用户。
@Entity
@Table(name = "member")
public class Member {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "first_name")
private String firstName;
}
频道 - 按主题分组博客帖子。
@Entity
@Table(name = "channel")
public class Channel {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "name")
private String name;
}
可以将成员分配到频道,从而成为频道中的成员。所以我们有ChannelMember实体,因为我们并不总是想要检索关系的两端。
@Entity
@Table(name = "channel_member")
public class ChannelMember {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToOne
@JoinColumn(name = "channel_id", nullable = false)
private Channel channel;
@OneToOne
@JoinColumn(name = "member_id", nullable = false)
private Member member;
}
我们使用Spring Data JPA和MySQL作为持久性(Spring Boot 1.5.3 Release)。
为什么在以下情况下,Hibernate的JPQL查询中的强制性必须能够获取任何记录?
存储库代码示例如下:
@Repository
public interface MemberRepository extends JpaRepository<Member, Long> {
@Query("select cm.member from ChannelMember cm")
List<Member> getNotWorking(); // empty List will be returned
@Query("select distinct cm.member from ChannelMember cm")
List<Member> getWorking();
}