Skip to main content

Overview

The User model is a JPA (Java Persistence API) entity that represents users in the EduTec system. It is mapped to the usuarios table in the database and uses Lombok annotations to reduce boilerplate code.

Database Mapping

  • Table Name: usuarios
  • Primary Key: id (auto-generated)
  • Unique Constraints: username must be unique

Fields

Long
required
Primary key identifier for the user. Auto-generated using the database’s identity strategy.Annotations:
  • @Id - Marks this field as the primary key
  • @GeneratedValue(strategy = GenerationType.IDENTITY) - Database auto-generates values
String
required
Unique username for the user account. Used for authentication and identification.Constraints:
  • Not null
  • Must be unique across all users
Annotations:
  • @Column(nullable = false, unique = true)
String
required
Encrypted password for user authentication. Should be hashed before storage.Constraints:
  • Not null
Annotations:
  • @Column(nullable = false)

JPA Annotations

Entity Configuration

  • @Entity - Marks this class as a JPA entity that will be managed by the persistence context
  • @Table(name = "usuarios") - Maps this entity to the usuarios table in the database

Field Annotations

  • @Id - Designates the primary key field
  • @GeneratedValue(strategy = GenerationType.IDENTITY) - Configures automatic ID generation using the database’s identity column
  • @Column - Specifies column constraints and properties
    • nullable = false - Field cannot be null in the database
    • unique = true - Field value must be unique across all records

Lombok Annotations

The User model uses Lombok annotations to automatically generate common code:
  • @Data - Generates getters, setters, toString(), equals(), and hashCode() methods
  • @NoArgsConstructor - Generates a no-argument constructor (required by JPA)
  • @AllArgsConstructor - Generates a constructor with all fields as parameters

Usage Example

Source Code

Database Table Structure

Security Considerations

The password field should never store plain text passwords. Always use a secure hashing algorithm (such as BCrypt) before saving passwords to the database.

Best Practices

  1. Password Hashing: Always hash passwords using Spring Security’s PasswordEncoder before persisting
  2. Validation: Consider adding @NotBlank and @Size annotations from javax.validation for input validation
  3. Security: Never expose password fields in API responses; use DTOs to transfer user data
  4. Indexing: The username field has a unique constraint, which automatically creates an index for efficient lookups