I am working on a project that involves managing calendar events and their associated members. I have several entities that represent a calendar event, the members associated with the event, and the relationships between them. Here are the main entities:
@Data
@Entity
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(exclude = "members")
@ToString(exclude = "members")
public class Calendar implements InProject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String description;
private LocalDateTime startDate;
private LocalDateTime endDate;
@ManyToOne
@JoinColumn(name = "type_of_work_id")
private TypeOfWork typeOfWork;
private String address;
@ManyToOne
@JoinColumn(name = "author_id")
private Account author;
@OneToMany(mappedBy = "id.calendarId", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
private Set<CalendarAccount> members = new HashSet<>();
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "project_id")
private Project project;
@Override
@Transactional
public void checkAccess(Account account, EntityAccessCheck accessCheck) {
accessCheck.checkAccessInProject(this, account);
}
}
The CalendarAccount entity represents the many-to-many relationship between a Calendar and an Account. It uses an embedded ID to store the IDs of both the Calendar and the Account.
@Data
@Entity
public class CalendarAccount {
@EmbeddedId
private CalendarAccountId id;
@Enumerated(EnumType.STRING)
private MemberStatus status;
@ManyToOne(fetch = FetchType.LAZY)
@MapsId("accountId")
private Account account;
@MapsId("calendarId")
@ManyToOne
private Calendar calendar;
}
The CalendarAccountId class is an embedded ID that stores the IDs of both the Calendar and the Account.
@Data
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
public class CalendarAccountId implements Serializable {
private Long calendarId;
private Long accountId;
}
I have a test that attempts to create a Calendar event and associate it with two Account members. However, the test fails because the members field is mandatory and cannot be initialized after saving the calendar. The issue is that I can’t create a CalendarAccountId if I don’t know the Calendar ID in advance, as it is generated by the database upon saving the Calendar entity.
The main issue is that the members field is mandatory and cannot be initialized after saving the calendar. However, I can’t create a CalendarAccountId if I don’t know the Calendar ID in advance, as it is generated by the database upon saving the Calendar entity. How can I solve this problem?