Skip to content

Commit 19c43fb

Browse files
committed
Remove AbstractAuditableEntity, move functions to AbstractEntity.
1 parent 0a2af39 commit 19c43fb

File tree

3 files changed

+125
-158
lines changed

3 files changed

+125
-158
lines changed

src/main/java/org/springframework/data/ebean/domain/AbstractAuditableEntity.java

Lines changed: 0 additions & 91 deletions
This file was deleted.

src/main/java/org/springframework/data/ebean/domain/AbstractEntity.java

Lines changed: 119 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,91 +16,149 @@
1616

1717
package org.springframework.data.ebean.domain;
1818

19+
import io.ebean.annotation.CreatedTimestamp;
20+
import io.ebean.annotation.UpdatedTimestamp;
21+
import io.ebean.annotation.WhoCreated;
22+
import io.ebean.annotation.WhoModified;
23+
import org.springframework.data.domain.Auditable;
1924
import org.springframework.data.domain.Persistable;
2025
import org.springframework.util.ClassUtils;
2126

2227
import javax.persistence.Id;
2328
import javax.persistence.MappedSuperclass;
2429
import javax.persistence.Transient;
30+
import java.time.LocalDateTime;
31+
import java.util.Optional;
2532

2633
/**
27-
* Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
34+
* Abstract base class for entities.
2835
* {@link #equals(Object)} and {@link #hashCode()} based on that id.
2936
*
3037
* @author Xuegui Yuan
3138
*/
3239
@MappedSuperclass
33-
public abstract class AbstractEntity implements Persistable<Long> {
40+
public abstract class AbstractEntity implements Persistable<Long>, Auditable<String, Long, LocalDateTime> {
3441

35-
private static final long serialVersionUID = -5554308939380869754L;
42+
private static final long serialVersionUID = -5554308939380869754L;
3643

3744
@Id
38-
protected Long id;
39-
40-
/*
41-
* (non-Javadoc)
42-
*
43-
* @see java.lang.Object#hashCode()
44-
*/
45-
@Override
46-
public int hashCode() {
47-
48-
int hashCode = 17;
49-
50-
hashCode += null == getId() ? 0 : getId().hashCode() * 31;
51-
52-
return hashCode;
53-
}
54-
55-
@Override
56-
public Long getId() {
57-
return id;
58-
}
59-
60-
/**
61-
* Sets the id of the entity.
62-
*
63-
* @param id the id to set
64-
*/
65-
public void setId(final Long id) {
66-
this.id = id;
67-
}
68-
69-
@Transient
70-
@Override
71-
public boolean isNew() {
72-
return null == getId();
73-
}
74-
75-
@Override
76-
public boolean equals(Object obj) {
77-
78-
if (null == obj) {
79-
return false;
45+
protected Long id;
46+
47+
@WhoCreated
48+
String createdBy;
49+
50+
@CreatedTimestamp
51+
LocalDateTime createdDate;
52+
53+
@WhoModified
54+
String lastModifiedBy;
55+
56+
@UpdatedTimestamp
57+
LocalDateTime lastModifiedDate;
58+
59+
@Override
60+
public Long getId() {
61+
return id;
62+
}
63+
64+
/**
65+
* Sets the id of the entity.
66+
*
67+
* @param id the id to set
68+
*/
69+
public void setId(final Long id) {
70+
this.id = id;
71+
}
72+
73+
@Transient
74+
@Override
75+
public boolean isNew() {
76+
return null == getId();
77+
}
78+
79+
@Override
80+
public Optional<String> getCreatedBy() {
81+
return Optional.ofNullable(createdBy);
82+
}
83+
84+
@Override
85+
public void setCreatedBy(String createdBy) {
86+
this.createdBy = createdBy;
8087
}
8188

82-
if (this == obj) {
83-
return true;
89+
@Override
90+
public Optional<LocalDateTime> getCreatedDate() {
91+
return Optional.ofNullable(createdDate);
8492
}
8593

86-
if (!getClass().equals(ClassUtils.getUserClass(obj))) {
87-
return false;
94+
@Override
95+
public void setCreatedDate(LocalDateTime createdDate) {
96+
this.createdDate = createdDate;
8897
}
8998

90-
AbstractEntity that = (AbstractEntity) obj;
99+
@Override
100+
public Optional<String> getLastModifiedBy() {
101+
return Optional.ofNullable(lastModifiedBy);
102+
}
91103

92-
return null == this.getId() ? false : this.getId().equals(that.getId());
93-
}
104+
@Override
105+
public void setLastModifiedBy(String lastModifiedBy) {
106+
this.lastModifiedBy = lastModifiedBy;
107+
}
94108

95-
/*
96-
* (non-Javadoc)
97-
*
98-
* @see java.lang.Object#toString()
99-
*/
100-
@Override
101-
public String toString() {
102-
return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId());
103-
}
109+
@Override
110+
public Optional<LocalDateTime> getLastModifiedDate() {
111+
return Optional.ofNullable(lastModifiedDate);
112+
}
113+
114+
@Override
115+
public void setLastModifiedDate(LocalDateTime lastModifiedDate) {
116+
this.lastModifiedDate = lastModifiedDate;
117+
}
118+
119+
120+
/*
121+
* (non-Javadoc)
122+
*
123+
* @see java.lang.Object#hashCode()
124+
*/
125+
@Override
126+
public int hashCode() {
127+
int hashCode = 17;
128+
129+
hashCode += null == getId() ? 0 : getId().hashCode() * 31;
130+
131+
return hashCode;
132+
}
133+
134+
@Override
135+
public boolean equals(Object obj) {
136+
if (null == obj) {
137+
return false;
138+
}
139+
140+
if (this == obj) {
141+
return true;
142+
}
143+
144+
if (!getClass().equals(ClassUtils.getUserClass(obj))) {
145+
return false;
146+
}
147+
148+
AbstractEntity that = (AbstractEntity) obj;
149+
150+
return null == this.getId() ? false : this.getId().equals(that.getId());
151+
}
152+
153+
/*
154+
* (non-Javadoc)
155+
*
156+
* @see java.lang.Object#toString()
157+
*/
158+
@Override
159+
public String toString() {
160+
return String.format("Entity of type %s with id: %s", this.getClass().getName(), getId());
161+
}
104162

105163

106164
}

src/test/java/org/springframework/data/ebean/repository/UserRepositoryIntegrationTests.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,12 @@ public void testFindByExample() {
130130
assertEquals(0, result2.size());
131131
}
132132

133-
// @Test
134-
// public void testAuditable() {
135-
// User u = userRepository.findUserByEmailAddressEqualsOql("[email protected]");
136-
// assertEquals("test", u.getCreatedBy().get());
137-
// assertEquals("test", u.getLastModifiedBy().get());
138-
// }
133+
@Test
134+
public void testAuditable() {
135+
User u = userRepository.findUserByEmailAddressEqualsOql("[email protected]");
136+
assertEquals("test", u.getCreatedBy().get());
137+
assertEquals("test", u.getLastModifiedBy().get());
138+
}
139139

140140
@Test
141141
public void testDomainEvent() {

0 commit comments

Comments
 (0)