簡體   English   中英

OneToMany Spring數據JDBC

[英]OneToMany Spring Data JDBC

我想用Spring Data JDBC建模OneToMany Relation。 我在這個非常有用的博客上閱讀了https://spring.io/blog/2018/09/24/spring-data-jdbc-references-and-aggregates ,當你想為ToMany參考建模時你應該使用引用:

因此,任何“多對一”和“多對多”關系都必須通過引用id來建模。

所以我有這個場景:
一名Student可以進行多次Registration 一個Registration只能有一個Student 如果刪除Registration ,則不應將已分配的Student刪除級聯。
我最終得到了這個建模:

@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Registration {

    private final @Id
    @Wither
    long registrationId;

    @NotNull
    private String electiveType;

    @NotNull
    private LocalDateTime created = LocalDateTime.now();

    @NotNull
    private StudentRegistrationReference studentRegistrationReference;

}

@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class StudentRegistrationReference {
    private long student;
    private long registration;
}

@Data
@AllArgsConstructor(access = AccessLevel.PRIVATE, onConstructor = @__(@PersistenceConstructor))
public class Student {

    private final @Id
    @Wither
    long studentId;

    @NotNull
    @Size(min = 4, max = 20)
    private String userId;

    @NotNull
    @Min(0)
    private int matriculationNumber;

    @NotNull
    @Email
    private String eMail;

    private Set<StudentRegistrationReference> studentRegistrationReferences = new HashSet<>();

}

我的問題是我的建模是否正確實施?

你引用的文章是在談論“很多到X”,但你會談論“X-To-Many”。 您可以使用直接引用或實體的列表/集/映射來建模一對一或一對多關系。

你應該避免的是雙向關系。 雖然你可能會讓它們與你正在使用的方法一起工作,但你真的不應該這樣做。

這帶來了一個問題:這個模型應該怎么樣?

做出的核心決定是涉及多少聚合?

Student肯定是一個聚合, Student類是它的聚合根。 它可以獨立存在。

Registration呢? 我認為,它可能是同一集合的一部分。 刪除測試很好。 如果你刪除一個Student從系統中,這樣做的注冊Student還有價值嗎? 或者應該與Student一起消失?

作為練習,讓我們做兩種變體。 我開始:只有一個聚合:

class Registration {

    @Id private long Id;

    String electiveType;
    LocalDateTime created = LocalDateTime.now();
}

class Student {

    @Id private long Id;

    String userId;
    int matriculationNumber;
    String eMail;
    Set<Registration> registrations = new HashSet<>();
}

有了這個,您將擁有一個存儲庫:

interface StudentRepository extends CrudRepository<Student, Long>{}

我刪除了所有Lombok注釋,因為它們與問題無關。 Spring Data JDBC可以在簡單屬性上運行。

如果RegistrationStudent都是聚合,則會更多地涉及:您需要決定哪一方擁有該引用。

第一種情況: Registration擁有參考。

class Registration {

    @Id private long Id;

    String electiveType;
    LocalDateTime created = LocalDateTime.now();

    Long studentId;
}

public class Student {

    @Id private long Id;

    String userId;
    int matriculationNumber;
    String eMail;
}

第二種情況: Student擁有參考

class Registration {

    @Id private long Id;

    String electiveType;
    LocalDateTime created = LocalDateTime.now();
}

class Student {

    @Id private long Id;

    String userId;
    int matriculationNumber;
    String eMail;

    Set<RegistrationRef> registrations = new HashSet<>();
}

class RegistrationRef {

    Long registrationId;
}

請注意, RegistrationRef沒有studentId或類似的。 registrations屬性假定的表將具有student_id列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM