Skip to content

Add Comment entity and CommentRepository - #28

Merged
annikaholmqvist94 merged 2 commits into
mainfrom
feat/comment-entity
Mar 27, 2026
Merged

Add Comment entity and CommentRepository#28
annikaholmqvist94 merged 2 commits into
mainfrom
feat/comment-entity

Conversation

@annikaholmqvist94

@annikaholmqvist94 annikaholmqvist94 commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

Closes #24

Summary by CodeRabbit

  • New Features
    • Users can now add comments to medical records with automatic tracking of creation and modification timestamps.
    • Comments are attributed to their authors and can be retrieved or filtered by medical record or author, with counts available per record.

@coderabbitai

coderabbitai Bot commented Mar 27, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9b900997-2431-4b72-b75e-9893fc4716e1

📥 Commits

Reviewing files that changed from the base of the PR and between 841ae13 and 240507a.

📒 Files selected for processing (1)
  • src/main/java/org/example/vet1177/entities/Comment.java
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/main/java/org/example/vet1177/entities/Comment.java

📝 Walkthrough

Walkthrough

Added a new JPA entity Comment (UUID id) with ManyToOne relations to MedicalRecord and User, text body, and automatic createdAt/updatedAt lifecycle timestamps; added a Spring Data JPA CommentRepository with query methods for record/author retrieval and counting.

Changes

Cohort / File(s) Summary
Comment Entity
src/main/java/org/example/vet1177/entities/Comment.java
New JPA entity mapped to comment table: UUID id (GenerationType.UUID), ManyToOne lazy associations medicalRecord (record_id, nullable=false, cascade REMOVE) and author (author_id, nullable=false, cascade REMOVE), body as TEXT (nullable=false), createdAt/updatedAt timestamps with @PrePersist/@PreUpdate, getters/setters.
Comment Repository
src/main/java/org/example/vet1177/repository/CommentRepository.java
New Spring Data JPA repository CommentRepository extends JpaRepository<Comment, UUID> with derived queries: findByMedicalRecordIdOrderByCreatedAtAsc(UUID), findByAuthorId(UUID), countByMedicalRecordId(UUID).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested labels

infrastruktur

Suggested reviewers

  • johanbriger
  • lindaeskilsson
  • TatjanaTrajkovic

Poem

🐰 A nibble of code I penned today,
Comments hop in, in neat display,
Linked to records, authors too,
Time-stamped whispers, fresh and new.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 18.75% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main changes: addition of a Comment entity and CommentRepository interface.
Linked Issues check ✅ Passed The pull request implements the primary objective from issue #24: adding a Comment entity and CommentRepository with appropriate JPA annotations and repository methods.
Out of Scope Changes check ✅ Passed All changes are directly related to the linked issue #24 objective of adding Comment entity and repository infrastructure with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/comment-entity

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (2)
src/main/java/org/example/vet1177/entities/Comment.java (1)

33-35: Use one clock read in @PrePersist for consistent initial timestamps.

This avoids tiny skew between createdAt and updatedAt on insert.

Proposed change
     `@PrePersist`
     protected void onCreate() {
-        createdAt = Instant.now();
-        updatedAt = Instant.now();
+        Instant now = Instant.now();
+        createdAt = now;
+        updatedAt = now;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/org/example/vet1177/entities/Comment.java` around lines 33 -
35, In Comment.onCreate (the `@PrePersist` hook) you should call Instant.now()
once and assign that single Instant to both createdAt and updatedAt to avoid
tiny skew between the two timestamps; locate the onCreate method in the Comment
class and replace the two separate Instant.now() calls with a single Instant now
= Instant.now(); used for both createdAt and updatedAt.
src/main/java/org/example/vet1177/repository/CommentRepository.java (1)

14-17: Consider pageable overloads for comment lookups to avoid unbounded fetches.

Both read methods currently load all rows into memory. Adding Pageable overloads gives safer scaling for active records/users without breaking existing callers.

Proposed change
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
 import org.springframework.data.jpa.repository.JpaRepository;
 import org.springframework.stereotype.Repository;

@@
     List<Comment> findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId);
+    Page<Comment> findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId, Pageable pageable);

@@
     List<Comment> findByAuthorId(UUID authorId);
+    Page<Comment> findByAuthorId(UUID authorId, Pageable pageable);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/main/java/org/example/vet1177/repository/CommentRepository.java` around
lines 14 - 17, The two repository methods
findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId) and findByAuthorId(UUID
authorId) can return unbounded result sets; add Pageable overloads to avoid
loading all rows into memory by providing methods like
findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId, Pageable pageable) and
findByAuthorId(UUID authorId, Pageable pageable) (prefer returning Page<Comment>
for pagination metadata), import org.springframework.data.domain.Page and
Pageable, and update callers to use pageable where appropriate to limit fetch
size.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/main/java/org/example/vet1177/entities/Comment.java`:
- Around line 26-30: Make the audit fields in Comment class persistence-managed
and non-nullable: add nullable = false to `@Column` on createdAt and updatedAt,
keep createdAt as updatable = false, remove or restrict any public setter for
updatedAt (make it private or package/protected) so callers cannot mutate it
directly, and ensure the entity lifecycle methods (e.g., `@PrePersist/`@PreUpdate
methods such as the existing setTimestamps or onCreate/onUpdate) are the sole
place that set createdAt and updatedAt; apply the same nullable=false and setter
restriction pattern to the other occurrence noted at the same symbol.
- Around line 15-21: The Comment entity’s foreign keys (fields medicalRecord and
author in class Comment) lack a parent-delete strategy; pick one approach and
implement it: either add cascade = CascadeType.REMOVE to the `@ManyToOne`
annotations on medicalRecord and author in Comment to cascade JPA deletes from
MedicalRecord/User, or modify the DDL in schema.sql to add ON DELETE CASCADE or
ON DELETE SET NULL to the comment table’s foreign key constraints for record_id
and author_id, or implement pre-delete cleanup methods in your services (e.g.,
add removeCommentsByRecordId(recordId) in MedicalRecordService and
removeCommentsByAuthorId(authorId) in UserService and call them before deleting
the parent). Ensure you update the referenced symbols Comment.medicalRecord,
Comment.author, schema.sql FK definitions, or MedicalRecordService/UserService
methods accordingly so deletes no longer violate FK constraints.

---

Nitpick comments:
In `@src/main/java/org/example/vet1177/entities/Comment.java`:
- Around line 33-35: In Comment.onCreate (the `@PrePersist` hook) you should call
Instant.now() once and assign that single Instant to both createdAt and
updatedAt to avoid tiny skew between the two timestamps; locate the onCreate
method in the Comment class and replace the two separate Instant.now() calls
with a single Instant now = Instant.now(); used for both createdAt and
updatedAt.

In `@src/main/java/org/example/vet1177/repository/CommentRepository.java`:
- Around line 14-17: The two repository methods
findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId) and findByAuthorId(UUID
authorId) can return unbounded result sets; add Pageable overloads to avoid
loading all rows into memory by providing methods like
findByMedicalRecordIdOrderByCreatedAtAsc(UUID recordId, Pageable pageable) and
findByAuthorId(UUID authorId, Pageable pageable) (prefer returning Page<Comment>
for pagination metadata), import org.springframework.data.domain.Page and
Pageable, and update callers to use pageable where appropriate to limit fetch
size.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fecdf39f-763a-4013-8dba-5f5717ea0757

📥 Commits

Reviewing files that changed from the base of the PR and between 028da4c and 841ae13.

📒 Files selected for processing (2)
  • src/main/java/org/example/vet1177/entities/Comment.java
  • src/main/java/org/example/vet1177/repository/CommentRepository.java

Comment thread src/main/java/org/example/vet1177/entities/Comment.java Outdated
Comment thread src/main/java/org/example/vet1177/entities/Comment.java Outdated
- Added `cascade=CascadeType.REMOVE` to `medicalRecord` and `author` relationships.
- Marked `createdAt` and `updatedAt` fields as `nullable = false`.
- Adjusted visibility of `setUpdatedAt` to `protected` for encapsulation improvement.

closes #9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Comment Entity and Repo

1 participant