我有表的表现和时间。一场演出可以在特定时间进行多次。一对多关系。
@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 中创建表 hours,其 PerformanceId 只有一个值。我如何插入同时播放 id 1,4,7 的表演的列表值。我需要额外的表hours_performance来存储hourId和perfomanceId吗?
最佳答案
由于您想要实现的关系是(根据我的理解)多对多,因此您确实需要第三个表将时间映射到关系。 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;
}
关于java - 将列表存储在表列中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50666220/