|
| 1 | +package com.baeldung.persistence.model; |
| 2 | + |
| 3 | +import com.google.common.collect.Sets; |
| 4 | +import org.baeldung.persistence.model.Foo; |
| 5 | +import org.hibernate.annotations.OrderBy; |
| 6 | +import org.hibernate.envers.Audited; |
| 7 | +import org.jboss.logging.Logger; |
| 8 | +import org.springframework.data.annotation.CreatedBy; |
| 9 | +import org.springframework.data.annotation.CreatedDate; |
| 10 | +import org.springframework.data.annotation.LastModifiedBy; |
| 11 | +import org.springframework.data.annotation.LastModifiedDate; |
| 12 | +import org.springframework.data.jpa.domain.support.AuditingEntityListener; |
| 13 | + |
| 14 | +import javax.persistence.*; |
| 15 | +import java.io.Serializable; |
| 16 | +import java.util.Date; |
| 17 | +import java.util.Set; |
| 18 | + |
| 19 | +@Entity |
| 20 | +@NamedQuery(name = "Bar.findAll", query = "SELECT b FROM Bar b") |
| 21 | +@Audited |
| 22 | +@EntityListeners(AuditingEntityListener.class) |
| 23 | +public class Bar implements Serializable { |
| 24 | + |
| 25 | + private static Logger logger = Logger.getLogger(Bar.class); |
| 26 | + |
| 27 | + public enum OPERATION { |
| 28 | + INSERT, UPDATE, DELETE; |
| 29 | + private String value; |
| 30 | + |
| 31 | + OPERATION() { |
| 32 | + value = toString(); |
| 33 | + } |
| 34 | + |
| 35 | + public String getValue() { |
| 36 | + return value; |
| 37 | + } |
| 38 | + |
| 39 | + public static OPERATION parse(final String value) { |
| 40 | + OPERATION operation = null; |
| 41 | + for (final OPERATION op : OPERATION.values()) { |
| 42 | + if (op.getValue().equals(value)) { |
| 43 | + operation = op; |
| 44 | + break; |
| 45 | + } |
| 46 | + } |
| 47 | + return operation; |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + @Id |
| 52 | + @GeneratedValue(strategy = GenerationType.AUTO) |
| 53 | + @Column(name = "id") |
| 54 | + private int id; |
| 55 | + |
| 56 | + @Column(name = "name") |
| 57 | + private String name; |
| 58 | + |
| 59 | + @OneToMany(mappedBy = "bar", cascade = CascadeType.ALL, fetch = FetchType.LAZY) |
| 60 | + @OrderBy(clause = "NAME DESC") |
| 61 | + // @NotAudited |
| 62 | + private Set<Foo> fooSet = Sets.newHashSet(); |
| 63 | + |
| 64 | + @Column(name = "operation") |
| 65 | + private String operation; |
| 66 | + |
| 67 | + @Column(name = "timestamp") |
| 68 | + private long timestamp; |
| 69 | + |
| 70 | + @Column(name = "created_date", updatable = false, nullable = false) |
| 71 | + @CreatedDate |
| 72 | + private long createdDate; |
| 73 | + |
| 74 | + @Column(name = "modified_date") |
| 75 | + @LastModifiedDate |
| 76 | + private long modifiedDate; |
| 77 | + |
| 78 | + @Column(name = "created_by") |
| 79 | + @CreatedBy |
| 80 | + private String createdBy; |
| 81 | + |
| 82 | + @Column(name = "modified_by") |
| 83 | + @LastModifiedBy |
| 84 | + private String modifiedBy; |
| 85 | + |
| 86 | + public Bar() { |
| 87 | + super(); |
| 88 | + } |
| 89 | + |
| 90 | + public Bar(final String name) { |
| 91 | + super(); |
| 92 | + |
| 93 | + this.name = name; |
| 94 | + } |
| 95 | + |
| 96 | + // API |
| 97 | + |
| 98 | + public Set<Foo> getFooSet() { |
| 99 | + return fooSet; |
| 100 | + } |
| 101 | + |
| 102 | + public void setFooSet(final Set<Foo> fooSet) { |
| 103 | + this.fooSet = fooSet; |
| 104 | + } |
| 105 | + |
| 106 | + public int getId() { |
| 107 | + return id; |
| 108 | + } |
| 109 | + |
| 110 | + public void setId(final int id) { |
| 111 | + this.id = id; |
| 112 | + } |
| 113 | + |
| 114 | + public String getName() { |
| 115 | + return name; |
| 116 | + } |
| 117 | + |
| 118 | + public void setName(final String name) { |
| 119 | + this.name = name; |
| 120 | + } |
| 121 | + |
| 122 | + public OPERATION getOperation() { |
| 123 | + return OPERATION.parse(operation); |
| 124 | + } |
| 125 | + |
| 126 | + public void setOperation(final OPERATION operation) { |
| 127 | + this.operation = operation.getValue(); |
| 128 | + } |
| 129 | + |
| 130 | + public long getTimestamp() { |
| 131 | + return timestamp; |
| 132 | + } |
| 133 | + |
| 134 | + public void setTimestamp(final long timestamp) { |
| 135 | + this.timestamp = timestamp; |
| 136 | + } |
| 137 | + |
| 138 | + public long getCreatedDate() { |
| 139 | + return createdDate; |
| 140 | + } |
| 141 | + |
| 142 | + public void setCreatedDate(final long createdDate) { |
| 143 | + this.createdDate = createdDate; |
| 144 | + } |
| 145 | + |
| 146 | + public long getModifiedDate() { |
| 147 | + return modifiedDate; |
| 148 | + } |
| 149 | + |
| 150 | + public void setModifiedDate(final long modifiedDate) { |
| 151 | + this.modifiedDate = modifiedDate; |
| 152 | + } |
| 153 | + |
| 154 | + public String getCreatedBy() { |
| 155 | + return createdBy; |
| 156 | + } |
| 157 | + |
| 158 | + public void setCreatedBy(final String createdBy) { |
| 159 | + this.createdBy = createdBy; |
| 160 | + } |
| 161 | + |
| 162 | + public String getModifiedBy() { |
| 163 | + return modifiedBy; |
| 164 | + } |
| 165 | + |
| 166 | + public void setModifiedBy(final String modifiedBy) { |
| 167 | + this.modifiedBy = modifiedBy; |
| 168 | + } |
| 169 | + |
| 170 | + public void setOperation(final String operation) { |
| 171 | + this.operation = operation; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public int hashCode() { |
| 176 | + final int prime = 31; |
| 177 | + int result = 1; |
| 178 | + result = prime * result + ((name == null) ? 0 : name.hashCode()); |
| 179 | + return result; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public boolean equals(final Object obj) { |
| 184 | + if (this == obj) |
| 185 | + return true; |
| 186 | + if (obj == null) |
| 187 | + return false; |
| 188 | + if (getClass() != obj.getClass()) |
| 189 | + return false; |
| 190 | + final Bar other = (Bar) obj; |
| 191 | + if (name == null) { |
| 192 | + if (other.name != null) |
| 193 | + return false; |
| 194 | + } else if (!name.equals(other.name)) |
| 195 | + return false; |
| 196 | + return true; |
| 197 | + } |
| 198 | + |
| 199 | + @Override |
| 200 | + public String toString() { |
| 201 | + final StringBuilder builder = new StringBuilder(); |
| 202 | + builder.append("Bar [name=").append(name).append("]"); |
| 203 | + return builder.toString(); |
| 204 | + } |
| 205 | + |
| 206 | + @PrePersist |
| 207 | + public void onPrePersist() { |
| 208 | + logger.info("@PrePersist"); |
| 209 | + audit(OPERATION.INSERT); |
| 210 | + } |
| 211 | + |
| 212 | + @PreUpdate |
| 213 | + public void onPreUpdate() { |
| 214 | + logger.info("@PreUpdate"); |
| 215 | + audit(OPERATION.UPDATE); |
| 216 | + } |
| 217 | + |
| 218 | + @PreRemove |
| 219 | + public void onPreRemove() { |
| 220 | + logger.info("@PreRemove"); |
| 221 | + audit(OPERATION.DELETE); |
| 222 | + } |
| 223 | + |
| 224 | + private void audit(final OPERATION operation) { |
| 225 | + setOperation(operation); |
| 226 | + setTimestamp((new Date()).getTime()); |
| 227 | + } |
| 228 | + |
| 229 | +} |
0 commit comments