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

Add address and postal code to user entity #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/main/java/com/springboot/blog/entity/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String name;

private String description;

@Column(unique = true, nullable = false)
private String slug;

@OneToMany(mappedBy = "category", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Post> posts;
}
13 changes: 13 additions & 0 deletions src/main/java/com/springboot/blog/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.Date;

Choose a reason for hiding this comment

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

Image description CodeRabbit

The import statement for java.util.Date has been added. However, it's recommended to use java.time.LocalDate or java.time.LocalDateTime instead of java.util.Date for date-time manipulation in modern Java applications. The newer classes provide a more comprehensive and user-friendly operation set.

- import java.util.Date;
+ import java.time.LocalDate;

import java.util.Set;

@Setter
Expand All @@ -19,14 +20,26 @@ public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String name;

@Column(nullable = false, unique = true)
private String username;

@Column(nullable = false, unique = true)
private String email;

@Column(nullable = false)
private String password;

private String address;

@Column(unique = true)
private String postalCode;

private Date birthDate;
Comment on lines +36 to +41

Choose a reason for hiding this comment

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

Image description CodeRabbit

New fields address, postalCode, and birthDate have been added to the User entity. However, there are no constraints on these fields. Consider adding appropriate constraints such as @Column(nullable = false) if these fields are mandatory. Also, making postalCode unique might not be suitable because multiple users can live in the same postal code area.

-    private String address;
+    @Column(nullable = false)
+    private String address;

-    @Column(unique = true)
-    private String postalCode;
+    private String postalCode;

-    private Date birthDate;
+    private LocalDate birthDate;


@ManyToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinTable(name = "users_roles",

Choose a reason for hiding this comment

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

Code Review:

Bug risks:

  1. Potentially missing nullability check for address field as it does not have the nullable = false attribute specified.
  2. The postalCode field is marked as unique, but postal codes are not always unique and may lead to constraint violations if not enforced properly.

Improvement suggestions:

  1. Consider adding validation annotations like @NotBlank or custom validations to ensure data integrity for fields like name, username, email, password, and potentially address.
  2. Documenting fields and methods in the code for better readability and maintainability.
  3. Add appropriate indexes on columns based on their usage in queries to optimize database performance.
  4. Use more specific fetch strategies other than FetchType.EAGER if possible to avoid potential performance issues when loading entities.
  5. Handle potential exceptions that might occur during database operations to improve error handling and maintain reliability.
  6. Consider refactoring code into smaller, more manageable classes if the User class becomes too large or complex.

Remember that further specific optimizations or improvements would depend on the context of how this class is being used within the application.

joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/springboot/blog/payload/CategoryDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
public class CategoryDto {
private Long id;
private String name;
private String slug;
private String description;
}
Loading