Overview
TheUser 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:
usernamemust 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
@Column(nullable = false, unique = true)
String
required
Encrypted password for user authentication. Should be hashed before storage.Constraints:
- Not null
@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 theusuariostable 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 propertiesnullable = false- Field cannot be null in the databaseunique = 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(), andhashCode()methods@NoArgsConstructor- Generates a no-argument constructor (required by JPA)@AllArgsConstructor- Generates a constructor with all fields as parameters
Usage Example
Related Endpoints
- POST /auth/login - Uses User model for authentication
Related Models
- LoginRequest - DTO used for user authentication
Source Code
Database Table Structure
Security Considerations
Best Practices
- Password Hashing: Always hash passwords using Spring Security’s
PasswordEncoderbefore persisting - Validation: Consider adding
@NotBlankand@Sizeannotations fromjavax.validationfor input validation - Security: Never expose password fields in API responses; use DTOs to transfer user data
- Indexing: The
usernamefield has a unique constraint, which automatically creates an index for efficient lookups