i am learning Spring JPA annotation and working on a project of spring boot for a social application: Technology stack:

  • Spring boot
  • Spring Security
  • JPA
  • MySQL database

So far i am able to create individual entities and fetch them using single where clauses and and operations.

I have Model 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;

}

I wish to write JPA Query, fetch all tweets from tweet table where i am following other users and current user is me.

0 Answers