summaryrefslogtreecommitdiff
path: root/microservices/03-resource-server/src/main/java/com/example/resourceserver/security/User.java
blob: 8f84297a901eecae82150467502e6ccb298ccd84 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.example.resourceserver.security;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.Table;

/*
    @Table - Used to set the database table name explicitely.
*/
@Entity
@Table(name = "user_account")
public class User {

    /*
     * GenerationType.IDENTITY - Auto generate value based on database's native auto
     * increment feature
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    @Column(nullable = false)
    private String description;

    /*
     * @ElementCollection: Marks the field as a collection of basic types (Strings,
     * Integers, etc.) or embeddable objects.
     * 
     * @CollectionTable: Customizes the junction table details. If omitted, JPA
     * generates a default table name combining the entity name and the field name
     * (e.g., User_roles).
     * 
     * @Column: Defines the column name for the String values inside the collection
     * table.
     * 
     * Fetch Type: By default, element collections use FetchType.LAZY. If you access
     * the set outside of an active transaction or Hibernate session, it throws a
     * LazyInitializationException. You can switch it to fetch = FetchType.EAGER to
     * load the strings immediately alongside the main entity.
     * 
     */
    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "user_roles", // Name of the separate collection table
            joinColumns = @JoinColumn(name = "user_id") // Foreign key linking back to this entity
    )
    @Column(name = "role_name") // Name of the column storing the actual String values
    private Set<String> roles = new HashSet<>(); // e.g., ["ADMIN", "USER"]

    public String getPassword() {
        return password;
    }

    public String getUsername() {
        return username;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<String> getRoles() {
        return roles;
    }

    public void setRoles(Set<String> roles) {
        this.roles = roles;
    }
}