0

Probably the question is not correct so I will try to explain the problem well. I'm developing a mini project with spring boot for the creation of a calendar in which any registered user can add events to his calendar.So at the moment I have a page in which I can see the list of all the inserted events, So at the moment I have a page in which I view the list of all the inserted events, but the events are retrieved via the findAll() method so each user sees all the events registered in the database. Obviously my goal is to recover from the db only the events of the specific logged in user.When I create an event the foreign key is the user's id so I just need to figure out how to build the query in spring.

This is my User.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "users")
public class User {
    private static final long serialVersionUID = 1L;

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

    @Column(nullable = false)
    private String name;

    @Column(nullable = false, unique = true)
    private String email;

    @Column(nullable = false)
    private String password;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private Set<Event> events = new HashSet<Event>();

}

This is my Event.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "events")
public class Event {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "event_id")
    private Long id;

    @Column(name = "event_title", nullable = false)
    @NotBlank(message = "Titolo evento obbligatorio")
    private String eventTitle;

    @Column(name = "event_description", nullable = false)
    private String eventDescription;

    @Column(name = "event_date", nullable = false)
    @NotBlank(message = "Data evento obbligatoria")
    private String eventDate;

    @Column(name = "start_time", nullable = false)
    @NotNull
    private int startTime;

    @Column(name = "end_time", nullable = false)
    @NotNull
    private int endTime;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    private User user;
}

EventRepository:

public interface EventRepository extends CrudRepository<Event, Long> {

}

Method in the controller to display the event list:

 @GetMapping("/day")
    public String calendar(@AuthenticationPrincipal UserDetails userDetails, Model model) {
        String email = userDetails.getUsername();
        User user = userService.findUserByEmail(email);
        model.addAttribute("user", user);
        model.addAttribute("events", eventRepository.findAll());
        return "day";
    }

I'm quite newbie with spring so I couldn't even find anything on the internet, thanks in advance for your help.

1 Answers1

0

In brief, you should be able to extend your EventRepository like so:

public interface EventRepository extends CrudRepository<Event, Long> {
  public Iterable<Event> findByUser(User user);
}

You'll then be able to call the findByUser method from your service by passing the user you want to query for.

I'd recommend taking a look at Spring JPA and its JpaRepository. It extends the functionality of CrudRepository with more options such as paging. It also returns List<> instead of Iterable<>, which if often useful. You can find further comparisons elsewhere on this site too.

JJ Gray
  • 51
  • 4