我们在项目中使用JPA2,Spring Data和QueryDSL.我有以下表格和相关的JPA实体:
table Person (id, ...)
table Activity (id, type, ...)
@Entity
@Configurable
public class Activity {
@ElementCollection
@CollectionTable(joinColumns = @JoinColumn(name = "ACTIVITY_ID"))
@NotEmpty
@Valid
private Set<ActivityName> names = new HashSet<>();
table ActivityName(activity_id, name, ...)
@Embeddable
@Immutable
@Table(uniqueConstraints = @UniqueConstraint(columnNames = "NAME"))
public static class ActivityName { ... }
table ActivityLevel(person_id, activity_id, level)
@Entity
@Immutable
@Validated
public final class ActivityLevel{...}
Run Code Online (Sandbox Code Playgroud)
1..n for Actitivy to ActivityName - 活动可能有不同的名称(例如跑步,慢跑)
一个人可能对某个特定活动具有一定的水平,并且可以执行多个活动(每个活动都有一个定义的级别).
示例以下数据: