J.T.提供的答案是正确的,但正如我和sebge2在他/她的评论中指出的那样,答案并不完整。的组合和进一步要求。@ElementCollection
@OnDelete
@JoinColumn()
后续示例:
@Entity
public class Report extends Model {
@Id
@Column(name = "report_id", columnDefinition = "BINARY(16)")
public UUID id; // Added for the sake of this entity having a primary key
public Date date;
public double availability;
@ElementCollection
@CollectionTable(name = "report_category", joinColumns = @JoinColumn(name = "report_id")) // choose the name of the DB table storing the Map<>
@MapKeyColumn(name = "fault_category_key") // choose the name of the DB column used to store the Map<> key
@Column(name = "fault_category_value") // choose the name of the DB column used to store the Map<> value
@JoinColumn(name = "report_id") // name of the @Id column of this entity
@OnDelete(action = OnDeleteAction.CASCADE)
@Cascade(value={CascadeType.ALL})
public Map<FaultCategory, Integer> categories;
}
此设置将创建一个名为的表和另一个包含三列的表:。和 之间的外键约束将为 。我用Map<String,String>测试了这个设置。report
report_category
report_id, fault_category_key, fault_category_value
report_category.report_id
report.report_id
ON DELETE CASCADE