我有桌面表演和时间。一个表演可以在某些时间播放多次。一对多的关系。
@Entity
@Table(name = "performance_type")
@Data
public class PerformanceType {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true)
private Integer performanceTypeId;
@Column(length=127)
private String performOptions;
}
和
@Entity
@Table(name = "performance")
@Data
public class Performance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true)
private Integer performanceId;
@OneToMany(mappedBy = "performance")
private List<Hours> performanceHours = new ArrayList<>();
}
在数据库hibernate中,创建具有performanceId的表时间只有一个值。我如何插入列表值,同时播放id为1,4,7的表演。我需要存储hourId和perfomanceId的附加表hours_performance?
答案 0 :(得分:1)
由于您希望实现的关系是(根据我的理解)多对多关系,因此您确实需要第三个表格将小时数映射到关系。 Here is a very good example.您需要设置第三个表,其中包含两个要连接的表的外键。
@Entity
@Table(name = "performance")
@Data
public class Performance {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true)
private Integer performanceId;
@ManyToMany
@JoinTable(
name = "hours_performance",
joinColumns = { @JoinColumn(name = "performance_id") },
inverseJoinColumns = { @JoinColumn(name = "hour_id") }
)
private List<Hours> performanceHours = new ArrayList<>();
}
@Entity
@Table(name = "hours")
@Data
public class Hours {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(nullable = false, unique = true)
private Integer hourId;
ZonedDateTime time;
@ManyToMany(mappedBy = "performanceHours")
private List<Performance> performances;
}