View Javadoc
1   /*
2    * Copyright (c) 2022-2023. Roland T. Lichti, Kaiserpfalz EDV-Service.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16   */
17  
18  package de.kaiserpfalzedv.commons.jpa;
19  
20  import java.time.OffsetDateTime;
21  import java.util.Objects;
22  
23  import org.hibernate.annotations.CreationTimestamp;
24  import org.hibernate.annotations.UpdateTimestamp;
25  
26  import de.kaiserpfalzedv.commons.api.resources.HasId;
27  import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
28  import jakarta.persistence.Column;
29  import jakarta.persistence.GeneratedValue;
30  import jakarta.persistence.Id;
31  import jakarta.persistence.MappedSuperclass;
32  import jakarta.persistence.Version;
33  import lombok.AllArgsConstructor;
34  import lombok.Builder;
35  import lombok.Getter;
36  import lombok.NoArgsConstructor;
37  import lombok.NonNull;
38  import lombok.Setter;
39  import lombok.ToString;
40  import lombok.experimental.SuperBuilder;
41  
42  @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "lombok provided superbuilder constructor.")
43  @MappedSuperclass
44  @SuperBuilder(toBuilder = true)
45  @AllArgsConstructor
46  @NoArgsConstructor
47  @Getter
48  @Setter
49  @ToString(onlyExplicitlyIncluded = true)
50  public abstract class AbstractJPAEntity implements HasId, Cloneable {
51      @Id
52      @GeneratedValue
53      @Column(name = "ID", nullable = false, updatable = false, unique = true)
54      @ToString.Include
55      protected Long id;
56  
57      @NonNull
58      @Version
59      @Column(name = "VERSION", nullable = false)
60      @Builder.Default
61      @ToString.Include
62      protected Integer version = 0;
63  
64      @CreationTimestamp
65      @Column(name = "CREATED", nullable = false, updatable = false)
66      @NonNull
67      protected OffsetDateTime created;
68  
69      @UpdateTimestamp
70      @Column(name = "MODIFIED", nullable = false)
71      @NonNull
72      protected OffsetDateTime modified;
73  
74      @Column(name = "DELETED", insertable = false)
75      protected OffsetDateTime deleted;
76  
77  
78      @Override
79      public boolean equals(final Object o) {
80          if (this == o) return true;
81          if (!(o instanceof final AbstractJPAEntity entity)) return false;
82          return this.id.equals(entity.getId());
83      }
84  
85      @Override
86      public int hashCode() {
87          return Objects.hash(this.getId());
88      }
89  
90      @Override
91      protected AbstractJPAEntity clone() throws CloneNotSupportedException {
92          final AbstractJPAEntity result = (AbstractJPAEntity) super.clone();
93  
94          result.id = this.id;
95          result.version = this.version;
96  
97          if (this.created != null) {
98              result.created = this.created;
99          }
100 
101         if (this.modified != null) {
102             result.modified = this.modified;
103         }
104 
105         return result;
106     }
107 }