Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Student, Instructor and User Entities for PostgreSQL Migration #12071

Merged

Conversation

domlimm
Copy link
Contributor

@domlimm domlimm commented Feb 10, 2023

Part of #12048.

Outline

This PR creates the new Student and Instructor Entity to be used for PostgreSQL Migration.

Below are the findings for the 4 available inheritance strategies in Hibernate:

MappedSuperclass

  • Not scalable. If we use this, ancestors cannot contain associations with other entities
  • Each DB table contains both base and sub classes properties

Single Table

  • Performant (polymorphic query performance) as only 1 table needs to be accessed when querying parent entities. Best performance among the other strategies
  • However, can no longer use NOT NULL constraints on subclass entity properties. I think this means that the identifying attributes of the rows of subclasses can be nullable?
  • For our case of Student/Instructor IS-A User, we will only have 1 table in the DB, i.e., User, with all the fields combined. This means Single Table is out of our options? Student is a User but it shouldn't have Instructor attributes/data.
  • Could use discriminator values which is used to differentiate between rows belonging to separate subclass types.

Joined Table aka Table-per-subclass mapping strategy (This has been selected)

  • TLDR: An inherited state is retrieved by joining with the table of the superclass
  • Great because the PK of User appears in its child classes, e.g., In Student/Instructor
  • Performance issue as retrieving entities requires joins between the tables, expensive for large number of records. Number of joins is higher when we query parent class as it will join with every single related child
  • Discriminator column not required. But each subclass must declare a table column holding the object identifier (which is kind of what we want as each subclass table contains the FKs to the base class, User; PKs of sub class tables are also FKs to the superclass table PK. If @PKJoinCol not set, the PK/FK cols are assumed to have the same names as the PK cols of primary table of the superclass)
  • When using polymorphic queries, base class table must be joined with all subclass tables to fetch every associated subclass instance

Table per Class

  • Performance issue because we union in the background when we query the base class i.e., User
  • Each table defines all persistent states of the class, including the inherited state.
  • If we want to use polymorphic associations (eg An association to the superclass of our hierarchy), we need to use union subclass mapping. This requires multiple UNION queries, be aware of performance implications of a large class hierarchy. Also, not all database systems support UNION ALL. PostgreSQL81Dialectk supports UNION ALL

@domlimm domlimm changed the title Create Student Entity for PostgreSQL Migration Create Student and Instructor Entity for PostgreSQL Migration Feb 11, 2023
@domlimm domlimm marked this pull request as ready for review February 12, 2023 10:11
Copy link
Member

@samuelfangjw samuelfangjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some progress, let's aim to finish this soon!

  1. Comment out code that will cause the build to fail.
  2. Add User, Instructor and Student to the list of annotated classes in HibernateUtil.java
  3. Please keep your branch updated with the v9 branch.

src/main/java/teammates/storage/sqlentity/Instructor.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlentity/Instructor.java Outdated Show resolved Hide resolved
Comment on lines 28 to 31
// Cascade?
@OneToOne
@JoinColumn(name = "teamId")
private Team team;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've decided to move this to User instead. Two reasons, firstly there is a little know "Instructors" team that is hardcoded. Secondly, this gives us the flexibility to support teams for instructors or teams with both student and instructors in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this is definitely not OneToOne. This is a ManyToOne relationship.

Copy link
Contributor Author

@domlimm domlimm Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense!

I've decided to move this to User instead. Two reasons, firstly there is a little know "Instructors" team that is hardcoded. Secondly, this gives us the flexibility to support teams for instructors or teams with both student and instructors in the future.

I think I misunderstood the direction. Please correct me if im wrong, from the Student's POV to have this attribute (previously), we read it as Many Students belong to One Team or One Team has Many Students? Hmmm so confusing! And as we were briefed, we should be going for bidirectional r/s too?

Also, this is definitely not OneToOne. This is a ManyToOne relationship.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this, might want to take a look at what each annotation means and at the differences between them. For best practices, I try to follow the ones defined here: https://thorben-janssen.com/best-practices-many-one-one-many-associations-mappings/.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will give it a read! 🙇🏻

src/main/java/teammates/storage/sqlentity/Student.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlentity/Student.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlentity/User.java Outdated Show resolved Hide resolved
src/main/java/teammates/storage/sqlentity/User.java Outdated Show resolved Hide resolved
@samuelfangjw
Copy link
Member

As for cascades, deleting an instructor/student should not cause anything else to be deleted. Deletion should not be cascaded.

@domlimm domlimm changed the title Create Student and Instructor Entity for PostgreSQL Migration Create Student, Instructor and User Entities for PostgreSQL Migration Feb 14, 2023
@samuelfangjw
Copy link
Member

@domlimm, before I take a look at this, please make sure the BE can be run without errors and the tables for the entities are correctly created in the db. Thanks!

Copy link
Member

@samuelfangjw samuelfangjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Getting closer, just a few more changes and we can merge

src/main/java/teammates/storage/sqlentity/Student.java Outdated Show resolved Hide resolved
@domlimm domlimm requested review from samuelfangjw and hhdqirui and removed request for samuelfangjw and hhdqirui February 15, 2023 13:57
Copy link
Member

@samuelfangjw samuelfangjw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some issues but not blockers, to fix in the next PR. I will merge this first as this is a potential blocker for others.

protected Instructor() {
// required by Hibernate
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing constructors for these entities

@samuelfangjw samuelfangjw merged commit 780101a into TEAMMATES:v9-migration Feb 15, 2023
domlimm added a commit to domlimm/teammates that referenced this pull request Feb 22, 2023
hhdqirui added a commit that referenced this pull request Mar 20, 2023
* [#12048] Set up github action workflows

* [#12048] v9: Skeleton implementation (#12056)

* [#12048] Add isMigrated flag to course (#12063)

* [#12048] Add is migrated flag to datastore account (#12070)

* Temporarily disable liquibase migrations

* [#12048] Create Notification Entity for PostgreSQL migration (#12061)

* [#12048] Create notification DB layer for v9 migration (#12075)

* [#12048] Add UsageStatistics entity and db (#12076)

* Migrate GetCourseAction.java

* Migrate DeleteCourseAction.java and relevant logic functions

* Migrate BinCourseAction.java and its related logic functions

* Update checkSpecificAccessControl functions in BinCourseAction and DeleteCourseAction classes

* Migrate RestoreCourseAction and its related logic functions

* Migrate UpdateCourseAction with its related logic functions

* [#12048] Add Account Entity (#12087)

* [#12048] Create SQL logic for CreateNotificationAction and add relevant tests for v9 migration (#12077)

* [#12048] Create Student, Instructor and User Entities for PostgreSQL Migration (#12071)

* [#12048] V9: Cleanup and refactor (#12090)

* Edit GetCourseAction and refactor out the old datastore code

* [#12048] Remove redundant InstructorRole Enum (#12091)

* Fix compilation error

* Update check for database to fetch from

* Add unit tests for CoursesDb

* [#12048] Update GetUsageStatisticsAction to include SQL entities (#12084)

* Add CoursesLogicTest class

* Disable failing tests

* Fix compilation error

* Fix Checkstyle errors

* Merge branch

* Change flow for updating courses.

* Update updateCourse JavaDoc comment.

* Update CreateCourseAction and related methods

* Update GetCourseAction.

* Update UpdateCourseAction

* Update BinCourseAction and RestoreCourseAction

* Update DeleteCourseAction

* Migrate GetCourseSectionNamesAction and related methods.

* Add Unit tests for Logic layer of Course.

* Fix Checkstyle errors

* Add unit test for GetCourseAction's execute function

* Add verify for CoursesDb unit tests and use assertNull and assertNotNull

* Move fetching of course to logic layer.

* Fix Checkstyle errors.

* Move canCreateCourse logic to logic layer.

* Change *CourseAction classes to use isCourseMigrated

* Fix CoursesLogic's initLogicDependencies method call

* Add unit tests for GetCourseAction.

* Remove commented out method.

* Add minimal unit tests for BinCourseAction, DeleteCourseAction and RestoreCourseAction.

* Add minimal unit tests for GetCourseSectionAction and UpdateCourseAction.

* Remove unused EntityType parameter.

* Add minimal unit tests for CreateCourseAction.

* Fix Checkstyle errors.

* Ignore all old datastore test cases for *CourseAction classes.

* Fix 'text' type to 'test'.

* Change binCourseToRecycleBin to return the binned course.

* Update moveCourseToRecycleBin test.

* Update test name.

---------

Co-authored-by: Samuel Fang <samuelfangjw@gmail.com>
Co-authored-by: dao ngoc hieu <53283766+daongochieu2810@users.noreply.github.com>
Co-authored-by: Samuel Fang <60355570+samuelfangjw@users.noreply.github.com>
Co-authored-by: wuqirui <53338059+hhdqirui@users.noreply.github.com>
Co-authored-by: Dominic Lim <46486515+domlimm@users.noreply.github.com>
samuelfangjw added a commit that referenced this pull request Mar 20, 2023
* [#12048] Set up github action workflows

* [#12048] v9: Skeleton implementation (#12056)

* [#12048] Add isMigrated flag to course (#12063)

* [#12048] Add is migrated flag to datastore account (#12070)

* Temporarily disable liquibase migrations

* [#12048] Create Notification Entity for PostgreSQL migration (#12061)

* [#12048] Create notification DB layer for v9 migration (#12075)

* [#12048] Add UsageStatistics entity and db (#12076)

* Migrate GetCourseAction.java

* Migrate DeleteCourseAction.java and relevant logic functions

* Migrate BinCourseAction.java and its related logic functions

* Update checkSpecificAccessControl functions in BinCourseAction and DeleteCourseAction classes

* Migrate RestoreCourseAction and its related logic functions

* Migrate UpdateCourseAction with its related logic functions

* [#12048] Add Account Entity (#12087)

* [#12048] Create SQL logic for CreateNotificationAction and add relevant tests for v9 migration (#12077)

* [#12048] Create Student, Instructor and User Entities for PostgreSQL Migration (#12071)

* [#12048] V9: Cleanup and refactor (#12090)

* Edit GetCourseAction and refactor out the old datastore code

* [#12048] Remove redundant InstructorRole Enum (#12091)

* Fix compilation error

* Update check for database to fetch from

* Add unit tests for CoursesDb

* [#12048] Update GetUsageStatisticsAction to include SQL entities (#12084)

* Add CoursesLogicTest class

* Disable failing tests

* Fix compilation error

* Fix Checkstyle errors

* Merge branch

* Change flow for updating courses.

* Update updateCourse JavaDoc comment.

* Update CreateCourseAction and related methods

* Update GetCourseAction.

* Update UpdateCourseAction

* Update BinCourseAction and RestoreCourseAction

* Update DeleteCourseAction

* Migrate GetCourseSectionNamesAction and related methods.

* Add Unit tests for Logic layer of Course.

* Fix Checkstyle errors

* Add unit test for GetCourseAction's execute function

* Add verify for CoursesDb unit tests and use assertNull and assertNotNull

* Move fetching of course to logic layer.

* Fix Checkstyle errors.

* Move canCreateCourse logic to logic layer.

* Change *CourseAction classes to use isCourseMigrated

* Fix CoursesLogic's initLogicDependencies method call

* Add unit tests for GetCourseAction.

* Remove commented out method.

* Add minimal unit tests for BinCourseAction, DeleteCourseAction and RestoreCourseAction.

* Add minimal unit tests for GetCourseSectionAction and UpdateCourseAction.

* Remove unused EntityType parameter.

* Add minimal unit tests for CreateCourseAction.

* Fix Checkstyle errors.

* Ignore all old datastore test cases for *CourseAction classes.

* Fix 'text' type to 'test'.

* Change binCourseToRecycleBin to return the binned course.

* Update moveCourseToRecycleBin test.

* Update test name.

---------

Co-authored-by: Samuel Fang <samuelfangjw@gmail.com>
Co-authored-by: dao ngoc hieu <53283766+daongochieu2810@users.noreply.github.com>
Co-authored-by: Samuel Fang <60355570+samuelfangjw@users.noreply.github.com>
Co-authored-by: wuqirui <53338059+hhdqirui@users.noreply.github.com>
Co-authored-by: Dominic Lim <46486515+domlimm@users.noreply.github.com>
samuelfangjw added a commit that referenced this pull request Apr 8, 2023
* [#12048] Set up github action workflows

* [#12048] v9: Skeleton implementation (#12056)

* [#12048] Add isMigrated flag to course (#12063)

* [#12048] Add is migrated flag to datastore account (#12070)

* Temporarily disable liquibase migrations

* [#12048] Create Notification Entity for PostgreSQL migration (#12061)

* [#12048] Create notification DB layer for v9 migration (#12075)

* [#12048] Add UsageStatistics entity and db (#12076)

* Migrate GetCourseAction.java

* Migrate DeleteCourseAction.java and relevant logic functions

* Migrate BinCourseAction.java and its related logic functions

* Update checkSpecificAccessControl functions in BinCourseAction and DeleteCourseAction classes

* Migrate RestoreCourseAction and its related logic functions

* Migrate UpdateCourseAction with its related logic functions

* [#12048] Add Account Entity (#12087)

* [#12048] Create SQL logic for CreateNotificationAction and add relevant tests for v9 migration (#12077)

* [#12048] Create Student, Instructor and User Entities for PostgreSQL Migration (#12071)

* [#12048] V9: Cleanup and refactor (#12090)

* Edit GetCourseAction and refactor out the old datastore code

* [#12048] Remove redundant InstructorRole Enum (#12091)

* Fix compilation error

* Update check for database to fetch from

* Add unit tests for CoursesDb

* [#12048] Update GetUsageStatisticsAction to include SQL entities (#12084)

* Add CoursesLogicTest class

* Disable failing tests

* Fix compilation error

* Fix Checkstyle errors

* Merge branch

* Change flow for updating courses.

* Update updateCourse JavaDoc comment.

* Update CreateCourseAction and related methods

* Update GetCourseAction.

* Update UpdateCourseAction

* Update BinCourseAction and RestoreCourseAction

* Update DeleteCourseAction

* Migrate GetCourseSectionNamesAction and related methods.

* Add Unit tests for Logic layer of Course.

* Fix Checkstyle errors

* Add unit test for GetCourseAction's execute function

* Add verify for CoursesDb unit tests and use assertNull and assertNotNull

* Move fetching of course to logic layer.

* Fix Checkstyle errors.

* Move canCreateCourse logic to logic layer.

* Change *CourseAction classes to use isCourseMigrated

* Fix CoursesLogic's initLogicDependencies method call

* Add unit tests for GetCourseAction.

* Remove commented out method.

* Add minimal unit tests for BinCourseAction, DeleteCourseAction and RestoreCourseAction.

* Add minimal unit tests for GetCourseSectionAction and UpdateCourseAction.

* Remove unused EntityType parameter.

* Add minimal unit tests for CreateCourseAction.

* Fix Checkstyle errors.

* Ignore all old datastore test cases for *CourseAction classes.

* Fix 'text' type to 'test'.

* Change binCourseToRecycleBin to return the binned course.

* Update moveCourseToRecycleBin test.

* Update test name.

---------

Co-authored-by: Samuel Fang <samuelfangjw@gmail.com>
Co-authored-by: dao ngoc hieu <53283766+daongochieu2810@users.noreply.github.com>
Co-authored-by: Samuel Fang <60355570+samuelfangjw@users.noreply.github.com>
Co-authored-by: wuqirui <53338059+hhdqirui@users.noreply.github.com>
Co-authored-by: Dominic Lim <46486515+domlimm@users.noreply.github.com>
@wkurniawan07 wkurniawan07 added s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging c.Task Other non-user-facing works, e.g. refactoring, adding tests labels Jan 21, 2024
@wkurniawan07 wkurniawan07 added this to the V9.0.0-beta.0 milestone Jan 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c.Task Other non-user-facing works, e.g. refactoring, adding tests s.ToMerge The PR is approved by all reviewers including final reviewer; ready for merging
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants