1
\$\begingroup\$

I've a User and Notification Entity relationship as:

@Entity
public class Notification implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;
    private String notification;
    @Temporal(TemporalType.TIMESTAMP)
    private Date sentDate;

    @ManyToMany(mappedBy = "notifications", cascade = {CascadeType.MERGE})          
    private List<User> users;
    // setter - getters methods are omitted
} 

and

@Entity
public class User implements Serializable {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        private long userId;
        private String userName;
        @Temporal(TemporalType.TIMESTAMP)
            private Date userJoinedAt;
        @ManyToMany(cascade = { CascadeType.PERSIST })
        @JoinTable              
        private List<Notification> notifications;
        // setter - getters methods are omitted
} 

I've to add messages to a new user record, who should only see the admin sent messages that are sent after his sign-up time.

I've tried:

Approach 1

public void addNotification(Notification notification) throws Exception {         
      notification.setNotificationSentDate(new Date());
      notificationService.save(notification);
      List<Notification> notifications = notificationService.getAll();            
      List<User> registeredUsers = userService.getAll();
      for (User user : registeredUsers) {
          List<Notification> afterJoinedNotifications = new CopyOnWriteArrayList<Notification>();             
          for (Notification savedNotification : notifications) {                  
              if(user.getUserJoinedAt().before(savedNotification.getNotificationSentDate())){                     
                  afterJoinedNotifications.add(savedNotification);                       
                    user.setNotifications(afterJoinedNotifications);                        
                    userService.save(user);
                  }
          }
      }   
  }

Approach 2

public void addNotification(Notification notification) throws Exception {         
      notification.setNotificationSentDate(new Date());
      notificationService.save(notification);
      List<Notification> notifications = notificationService.getAll();            
      List<User> registeredUsers = userService.getAll();
      for (User user : registeredUsers) {

          for (Notification savedNotification : notifications) {              
              if(user.getUserJoinedAt().before(savedNotification.getNotificationSentDate())){                                            
                    user.addNotification(savedNotification);                        
                    userService.save(user);
                  }
          }
      }   
  }

and added a new method to User Entity in the case of Approach 2 as:

public void addNotification(Notification notification) {
        notifications.add(notification);
    }

Note: this app is a very complex system, which is working mostly on real-time notifications. I want to have a best suite approach for that system. Which one I should take from above? Or is there another approach which best suites the scenario?

\$\endgroup\$
4
  • \$\begingroup\$ Are you on Java 8? \$\endgroup\$ – h.j.k. May 20 '16 at 14:06
  • 1
    \$\begingroup\$ @h.j.k. not I'm on Java 7! \$\endgroup\$ – Arshad Ali May 21 '16 at 2:03
  • 1
    \$\begingroup\$ Can you actually provide the setters implementations at least? They play a big part in influencing which approach is not recommended (I already have on my mind which is recommended in any case though). \$\endgroup\$ – h.j.k. May 21 '16 at 4:30
  • \$\begingroup\$ You've been asked for additional context by @h.j.k. 3 years ago. Without additional context your question risks being closed. The context is important for the answer to your question. \$\endgroup\$ – Mast Aug 7 '19 at 6:45