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 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
| @MappedSuperclass @Data @NoArgsConstructor @AllArgsConstructor @EntityListeners(AuditingEntityListener.class) public abstract class BaseEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @CreatedDate @Column(name = "created_at", nullable = false, updatable = false) private LocalDateTime createdAt; @LastModifiedDate @Column(name = "updated_at") private LocalDateTime updatedAt; @CreatedBy @Column(name = "created_by", updatable = false) private String createdBy; @LastModifiedBy @Column(name = "updated_by") private String updatedBy; @Version private Long version; @Column(name = "deleted", nullable = false) private Boolean deleted = false; }
@Entity @Table(name = "users", indexes = { @Index(name = "idx_username", columnList = "username", unique = true), @Index(name = "idx_email", columnList = "email", unique = true), @Index(name = "idx_created_at", columnList = "created_at") }) @Data @EqualsAndHashCode(callSuper = true) @NoArgsConstructor @AllArgsConstructor @Builder @Where(clause = "deleted = false") @SQLDelete(sql = "UPDATE users SET deleted = true WHERE id = ?") public class User extends BaseEntity { @Column(name = "username", nullable = false, unique = true, length = 50) private String username; @Column(name = "password", nullable = false) private String password; @Column(name = "email", nullable = false, unique = true, length = 100) private String email; @Column(name = "nickname", length = 100) private String nickname; @Column(name = "avatar") private String avatar; @Column(name = "phone", length = 11) private String phone; @Column(name = "active", nullable = false) private Boolean active = true; @Column(name = "last_login_at") private LocalDateTime lastLoginAt; @Column(name = "login_count", nullable = false) private Integer loginCount = 0; @ManyToMany(fetch = FetchType.LAZY) @JoinTable( name = "user_roles", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id") ) private Set<Role> roles = new HashSet<>(); @OneToMany(mappedBy = "author", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<Post> posts = new ArrayList<>(); public void addRole(Role role) { this.roles.add(role); role.getUsers().add(this); } public void removeRole(Role role) { this.roles.remove(role); role.getUsers().remove(this); } public boolean hasRole(String roleName) { return roles.stream() .anyMatch(role -> role.getName().equals(roleName)); } public void updateLoginInfo() { this.lastLoginAt = LocalDateTime.now(); this.loginCount++; } }
@Entity @Table(name = "roles") @Data @EqualsAndHashCode(callSuper = true, exclude = {"users", "permissions"}) @NoArgsConstructor @AllArgsConstructor @Builder public class Role extends BaseEntity { @Column(name = "name", nullable = false, unique = true, length = 50) private String name; @Column(name = "description") private String description; @ManyToMany(mappedBy = "roles", fetch = FetchType.LAZY) private Set<User> users = new HashSet<>(); @ManyToMany(fetch = FetchType.LAZY) @JoinTable( name = "role_permissions", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id") ) private Set<Permission> permissions = new HashSet<>(); }
@Entity @Table(name = "posts", indexes = { @Index(name = "idx_title", columnList = "title"), @Index(name = "idx_status", columnList = "status"), @Index(name = "idx_created_at", columnList = "created_at") }) @Data @EqualsAndHashCode(callSuper = true, exclude = {"author", "tags", "comments"}) @NoArgsConstructor @AllArgsConstructor @Builder @Where(clause = "deleted = false") @SQLDelete(sql = "UPDATE posts SET deleted = true WHERE id = ?") public class Post extends BaseEntity { @Column(name = "title", nullable = false, length = 200) private String title; @Column(name = "content", columnDefinition = "TEXT") private String content; @Column(name = "summary", length = 500) private String summary; @Column(name = "cover_image") private String coverImage; @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) private PostStatus status = PostStatus.DRAFT; @Column(name = "view_count", nullable = false) private Integer viewCount = 0; @Column(name = "like_count", nullable = false) private Integer likeCount = 0; @Column(name = "comment_count", nullable = false) private Integer commentCount = 0; @Column(name = "published_at") private LocalDateTime publishedAt; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "author_id", nullable = false) private User author; @ManyToMany(fetch = FetchType.LAZY) @JoinTable( name = "post_tags", joinColumns = @JoinColumn(name = "post_id"), inverseJoinColumns = @JoinColumn(name = "tag_id") ) private Set<Tag> tags = new HashSet<>(); @OneToMany(mappedBy = "post", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List<Comment> comments = new ArrayList<>(); public enum PostStatus { DRAFT, PUBLISHED, ARCHIVED } public void publish() { this.status = PostStatus.PUBLISHED; this.publishedAt = LocalDateTime.now(); } public void incrementViewCount() { this.viewCount++; } public void incrementLikeCount() { this.likeCount++; } public void incrementCommentCount() { this.commentCount++; } }
|