有三张桌子。第一个带有日期的日历。
@Entity
@NamedEntityGraph(
name = "eventCalendar.events",
attributeNodes = @NamedAttributeNode("events"),
subgraphs = {
@NamedSubgraph(
name = "event-with-contacts-and-eventContents",
type = Event.class,
attributeNodes = { @NamedAttributeNode("contacts"),
@NamedAttributeNode("eventContents") })})
public class EventCalendar extends BaseEntity {
private Long day;
@Column(columnDefinition = "boolean default false")
private Boolean isActive;
@OneToMany(mappedBy = "eventCalendar",cascade = CascadeType.ALL)
private List<Event> events = new ArrayList<>();
}
每个日期可以有多个事件。
@Entity
@NamedEntityGraph(name = "event-with-contacts-and-eventContents",
attributeNodes = { @NamedAttributeNode("contacts"), @NamedAttributeNode("eventContents") })
public class Event extends BaseEntity {
@OneToMany(mappedBy = "event",cascade = CascadeType.ALL)
private List<EventContent> eventContents = new ArrayList<>();
@ManyToOne
@JoinColumn(name = "event_calendar_id")
private EventCalendar eventCalendar;
@ManyToMany
private Set<Contact> contacts = new LinkedHashSet<>();
}
每个事件可以有很多参与者,就像一个参与者可以在许多事件的列表中一样
@Entity
public class Contact extends BaseEntity {
private String surname;
private String name;
private String patronymic;
@ManyToMany
private Set<Event> events = new LinkedHashSet<>();
}
日历存储库
@Repository
public interface EventCalendarRepository extends BaseRepository<EventCalendar> {
@EntityGraph(value = "eventCalendar.events", type = EntityGraph.EntityGraphType.LOAD)
List<EventCalendar> findByDayIn(Long[] days);
}
eventContents 字段已加载,但未加载联系人。怎么修?
多对多关系需要显式描述表,否则hibernate会创建两个表。