JPA提取用户及其关注的所有供稿

时间:2019-04-11 10:45:42

标签: spring-boot spring-data-jpa

我正在学习Spring JPA批注,并致力于社交应用程序的Spring Boot项目: 技术堆栈:

  • 春季靴子
  • Spring Security
  • JPA
  • MySQL数据库

到目前为止,我已经能够创建单个实体并使用单个where子句和操作来获取它们。

我有模特 User.class

    @NoArgsConstructor
@Entity
@Table(name = "users")
public class User extends Auditable<String> implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    private Long id;


    @Getter
    @Setter
    private String username;

    @Getter
    @Setter
    private String name;

    @Getter
    @Setter
    private String avatar;

    @Getter
    @Setter
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Tweet> tweets;

    @Getter
    @Setter
    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Like> likes;

    @Getter
    @Setter
    @OneToMany(mappedBy = "followerUser", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Follower> followers;

    @Getter
    @Setter
    @OneToMany(mappedBy = "followingUser", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnore
    private Set<Following> following;


    @Getter
    @Setter
    @ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinTable(name = "user_role", joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    @JsonIgnore
    private Set<Role> roles;


    @Getter
    @Setter
    @Transient
    @JsonIgnore
    private List<GrantedAuthority> authorities;


    public User(String username) {
        this.username = username;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return null;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isEnabled() {
        return true;
    }
}

Tweet.class

@NoArgsConstructor
@Entity
@Table(name = "tweets")
public class Tweet extends Auditable<String> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    private Long id;

    @Getter
    @Setter
    @Column(columnDefinition = "TEXT")
    private String content;

    @Getter
    @Setter
    @ManyToOne
    @JoinColumn(name = "user")
    private User user;

    @Getter
    @Setter
    @OneToMany(mappedBy = "tweet", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<Like> likes;

}

Following.class

@Entity
@Table(name = "following")
public class Following extends Auditable<String> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    private Long id;

    @Getter
    @Setter
    @ManyToOne
    private User followingUser;

}

我希望编写JPA查询,从我正在关注其他用户且当前用户是我的tweet表中获取所有tweet。

0 个答案:

没有答案