Added additional integration test provided by Fabio Heer
[hibernate4-maven-plugin] / src / it / multimodule / shared / src / main / java / ch / dvbern / demo / entities / AbstractEntity.java
1 package ch.dvbern.demo.entities;
2
3 import java.io.Serializable;
4 import java.util.Objects;
5 import java.util.UUID;
6 import javax.annotation.Nonnull;
7 import javax.annotation.Nullable;
8 import javax.persistence.Column;
9 import javax.persistence.Id;
10 import javax.persistence.MappedSuperclass;
11 import javax.persistence.Version;
12 import javax.validation.constraints.NotNull;
13 import javax.validation.constraints.Size;
14
15 import org.apache.commons.lang3.builder.ToStringBuilder;
16 import org.hibernate.Hibernate;
17
18 import ch.dvbern.demo.util.Constants;
19
20 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21
22 @SuppressWarnings("ClassReferencesSubclass")
23 @MappedSuperclass
24 public abstract class AbstractEntity implements Serializable {
25
26         private static final long serialVersionUID = -979317154050183445L;
27
28         @Id
29         @Column(unique = true, nullable = false, updatable = false, length = Constants.UUID_LENGTH)
30         @Size(min = Constants.UUID_LENGTH, max = Constants.UUID_LENGTH)
31         private String id;
32
33         @Version
34         @NotNull
35         private long version;
36
37         @Size(max = Constants.UUID_LENGTH)
38         @Column(nullable = false, length = Constants.UUID_LENGTH)
39         private String userErstellt;
40
41         @Size(max = Constants.UUID_LENGTH)
42         @Column(nullable = false, length = Constants.UUID_LENGTH)
43         private String userMutiert;
44
45         protected AbstractEntity() {
46                 //da wir teilweise schon eine id brauchen bevor die Entities gespeichert werden initialisieren wir die uuid hier
47                 id = UUID.randomUUID().toString();
48         }
49
50         public String getId() {
51                 return id;
52         }
53
54         public void setId(@Nullable String id) {
55                 this.id = id;
56         }
57
58         // Nullable, da erst im PrePersist gesetzt
59         public long getVersion() {
60                 return version;
61         }
62
63         public void setVersion(long version) {
64                 this.version = version;
65         }
66
67         @Nullable // Nullable, da erst im PrePersist gesetzt
68         public String getUserErstellt() {
69                 return userErstellt;
70         }
71
72         public void setUserErstellt(@Nonnull String userErstellt) {
73                 this.userErstellt = userErstellt;
74         }
75
76         @Nullable // Nullable, da erst im PrePersist gesetzt
77         public String getUserMutiert() {
78                 return userMutiert;
79         }
80
81         public void setUserMutiert(@Nonnull String userMutiert) {
82                 this.userMutiert = userMutiert;
83         }
84
85         @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
86         @SuppressFBWarnings(value = "BC_EQUALS_METHOD_SHOULD_WORK_FOR_ALL_OBJECTS", justification = "Es wird Hibernate.getClass genutzt um von Proxies (LazyInit) die konkrete Klasse zu erhalten")
87         @Override
88         public boolean equals(@Nullable Object o) {
89                 if (this == o) {
90                         return true;
91                 }
92
93                 if (o == null || Hibernate.getClass(this) != Hibernate.getClass(o)) {
94                         return false;
95                 }
96
97                 AbstractEntity that = (AbstractEntity) o;
98
99                 Objects.requireNonNull(getId());
100                 Objects.requireNonNull(that.getId());
101
102                 return getId().equals(that.getId());
103         }
104
105         public int hashCode() {
106                 return getId() != null ? getId().hashCode() : 0;
107         }
108
109         @Override
110         public String toString() {
111                 return new ToStringBuilder(this)
112                         .append("id", getId())
113                         .toString();
114         }
115 }