Add Comment entity and CommentRepository - #28
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded a new JPA entity Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
src/main/java/org/example/vet1177/entities/Comment.java (1)
33-35: Use one clock read in@PrePersistfor consistent initial timestamps.This avoids tiny skew between
createdAtandupdatedAton 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
Pageableoverloads 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
📒 Files selected for processing (2)
src/main/java/org/example/vet1177/entities/Comment.javasrc/main/java/org/example/vet1177/repository/CommentRepository.java
- 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
Closes #24
Summary by CodeRabbit