Skip to content

Commit 2e6bc30

Browse files
author
dupirefr
committed
[BAEL-2048] Auditing with Spring Data JPA article
1 parent 784d99e commit 2e6bc30

11 files changed

Lines changed: 852 additions & 0 deletions

File tree

‎spring-data-jpa/pom.xml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@
2121
<groupId>org.hibernate</groupId>
2222
<artifactId>hibernate-ehcache</artifactId>
2323
</dependency>
24+
<dependency>
25+
<groupId>org.hibernate</groupId>
26+
<artifactId>hibernate-envers</artifactId>
27+
</dependency>
2428
<dependency>
2529
<groupId>com.h2database</groupId>
2630
<artifactId>h2</artifactId>
2731
</dependency>
2832

33+
<dependency>
34+
<groupId>org.springframework.security</groupId>
35+
<artifactId>spring-security-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
2939
<dependency>
3040
<groupId>com.google.guava</groupId>
3141
<artifactId>guava</artifactId>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.persistence.dao;
2+
3+
import com.baeldung.persistence.model.Bar;
4+
import org.springframework.data.repository.CrudRepository;
5+
6+
import java.io.Serializable;
7+
8+
public interface IBarCrudRepository extends CrudRepository<Bar, Serializable> {
9+
//
10+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.persistence.dao.common;
2+
3+
import java.io.Serializable;
4+
import java.util.List;
5+
6+
public interface IOperations<T extends Serializable> {
7+
8+
T findOne(final long id);
9+
10+
List<T> findAll();
11+
12+
void create(final T entity);
13+
14+
T update(final T entity);
15+
16+
void delete(final T entity);
17+
18+
void deleteById(final long entityId);
19+
20+
}
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.baeldung.persistence.model;
2+
3+
import org.hibernate.envers.Audited;
4+
5+
import javax.persistence.*;
6+
import java.io.Serializable;
7+
8+
@NamedNativeQueries({ @NamedNativeQuery(name = "callGetAllFoos", query = "CALL GetAllFoos()", resultClass = Foo.class), @NamedNativeQuery(name = "callGetFoosByName", query = "CALL GetFoosByName(:fooName)", resultClass = Foo.class) })
9+
@Entity
10+
@Audited
11+
// @Proxy(lazy = false)
12+
public class Foo implements Serializable {
13+
14+
@Id
15+
@GeneratedValue(strategy = GenerationType.AUTO)
16+
@Column(name = "id")
17+
private long id;
18+
19+
@Column(name = "name")
20+
private String name;
21+
22+
@ManyToOne(targetEntity = Bar.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
23+
@JoinColumn(name = "BAR_ID")
24+
private Bar bar = new Bar();
25+
26+
public Foo() {
27+
super();
28+
}
29+
30+
public Foo(final String name) {
31+
super();
32+
this.name = name;
33+
}
34+
35+
//
36+
37+
public Bar getBar() {
38+
return bar;
39+
}
40+
41+
public void setBar(final Bar bar) {
42+
this.bar = bar;
43+
}
44+
45+
public long getId() {
46+
return id;
47+
}
48+
49+
public void setId(final long id) {
50+
this.id = id;
51+
}
52+
53+
public String getName() {
54+
return name;
55+
}
56+
57+
public void setName(final String name) {
58+
this.name = name;
59+
}
60+
61+
//
62+
63+
@Override
64+
public int hashCode() {
65+
final int prime = 31;
66+
int result = 1;
67+
result = prime * result + ((name == null) ? 0 : name.hashCode());
68+
return result;
69+
}
70+
71+
@Override
72+
public boolean equals(final Object obj) {
73+
if (this == obj)
74+
return true;
75+
if (obj == null)
76+
return false;
77+
if (getClass() != obj.getClass())
78+
return false;
79+
final Foo other = (Foo) obj;
80+
if (name == null) {
81+
if (other.name != null)
82+
return false;
83+
} else if (!name.equals(other.name))
84+
return false;
85+
return true;
86+
}
87+
88+
@Override
89+
public String toString() {
90+
final StringBuilder builder = new StringBuilder();
91+
builder.append("Foo [name=").append(name).append("]");
92+
return builder.toString();
93+
}
94+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.persistence.service;
2+
3+
import com.baeldung.persistence.dao.common.IOperations;
4+
import com.baeldung.persistence.model.Bar;
5+
6+
public interface IBarService extends IOperations<Bar> {
7+
//
8+
}

0 commit comments

Comments
 (0)