Skip to content

Commit 0a2af39

Browse files
committed
Add AbstractAggregateRoot base class.
1 parent 848bbaa commit 0a2af39

File tree

4 files changed

+84
-57
lines changed

4 files changed

+84
-57
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.data.ebean.domain;
18+
19+
import org.springframework.data.domain.AfterDomainEventPublication;
20+
import org.springframework.data.domain.DomainEvents;
21+
import org.springframework.util.Assert;
22+
23+
import javax.persistence.MappedSuperclass;
24+
import javax.persistence.Transient;
25+
import java.util.ArrayList;
26+
import java.util.Collection;
27+
import java.util.Collections;
28+
import java.util.List;
29+
30+
/**
31+
* Abstract base class for aggregate root, aggregate extends {@link AbstractEntity} and add
32+
* domain event functions.
33+
*
34+
* @author XueguiYuan
35+
*/
36+
@MappedSuperclass
37+
public abstract class AbstractAggregateRoot extends AbstractEntity {
38+
@Transient
39+
private transient final List<Object> domainEvents = new ArrayList<>();
40+
41+
/**
42+
* Registers the given event object for publication on a call to a Spring Data repository's save methods.
43+
*
44+
* @param event must not be {@literal null}.
45+
* @return the event that has been added.
46+
*/
47+
protected <T> T registerEvent(T event) {
48+
Assert.notNull(event, "Domain event must not be null!");
49+
50+
this.domainEvents.add(event);
51+
return event;
52+
}
53+
54+
/**
55+
* Clears all domain events currently held. Usually invoked by the infrastructure in place in Spring Data
56+
* repositories.
57+
*/
58+
@AfterDomainEventPublication
59+
protected void clearDomainEvents() {
60+
this.domainEvents.clear();
61+
}
62+
63+
/**
64+
* All domain events currently captured by the aggregate.
65+
*/
66+
@DomainEvents
67+
protected Collection<Object> domainEvents() {
68+
return Collections.unmodifiableList(domainEvents);
69+
}
70+
71+
}

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

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,12 @@
1616

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

19-
import org.springframework.data.domain.AfterDomainEventPublication;
20-
import org.springframework.data.domain.DomainEvents;
2119
import org.springframework.data.domain.Persistable;
22-
import org.springframework.util.Assert;
2320
import org.springframework.util.ClassUtils;
2421

2522
import javax.persistence.Id;
2623
import javax.persistence.MappedSuperclass;
2724
import javax.persistence.Transient;
28-
import java.util.ArrayList;
29-
import java.util.Collection;
30-
import java.util.Collections;
31-
import java.util.List;
3225

3326
/**
3427
* Abstract base class for entities. Allows parameterization of id type, chooses auto-generation and implements
@@ -40,42 +33,9 @@
4033
public abstract class AbstractEntity implements Persistable<Long> {
4134

4235
private static final long serialVersionUID = -5554308939380869754L;
43-
@Transient
44-
private transient final @org.springframework.data.annotation.Transient
45-
List<Object> domainEvents = new ArrayList<>();
46-
@Id
47-
protected Long id;
48-
49-
/**
50-
* Registers the given event object for publication on a call to a Spring Data repository's save methods.
51-
*
52-
* @param event must not be {@literal null}.
53-
* @return the event that has been added.
54-
*/
55-
protected <T> T registerEvent(T event) {
56-
57-
Assert.notNull(event, "Domain event must not be null!");
58-
59-
this.domainEvents.add(event);
60-
return event;
61-
}
6236

63-
/**
64-
* Clears all domain events currently held. Usually invoked by the infrastructure in place in Spring Data
65-
* repositories.
66-
*/
67-
@AfterDomainEventPublication
68-
protected void clearDomainEvents() {
69-
this.domainEvents.clear();
70-
}
71-
72-
/**
73-
* All domain events currently captured by the aggregate.
74-
*/
75-
@DomainEvents
76-
protected Collection<Object> domainEvents() {
77-
return Collections.unmodifiableList(domainEvents);
78-
}
37+
@Id
38+
protected Long id;
7939

8040
/*
8141
* (non-Javadoc)

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

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.springframework.data.ebean.repository;
22

3-
import java.util.List;
43
import org.junit.Before;
54
import org.junit.Test;
65
import org.junit.runner.RunWith;
@@ -13,11 +12,10 @@
1312
import org.springframework.test.context.ContextConfiguration;
1413
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1514

15+
import java.util.List;
16+
1617
import static org.hamcrest.CoreMatchers.hasItem;
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertNull;
20-
import static org.junit.Assert.assertThat;
18+
import static org.junit.Assert.*;
2119

2220
/**
2321
* @author Xuegui Yuan
@@ -132,12 +130,12 @@ public void testFindByExample() {
132130
assertEquals(0, result2.size());
133131
}
134132

135-
@Test
136-
public void testAuditable() {
137-
User u = userRepository.findUserByEmailAddressEqualsOql("[email protected]");
138-
assertEquals("test", u.getCreatedBy().get());
139-
assertEquals("test", u.getLastModifiedBy().get());
140-
}
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+
// }
141139

142140
@Test
143141
public void testDomainEvent() {

src/test/java/org/springframework/data/ebean/sample/domain/User.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
import lombok.Getter;
2020
import lombok.Setter;
21-
import org.springframework.data.ebean.domain.AbstractAuditableEntity;
22-
import org.springframework.data.ebean.domain.AggregateRoot;
21+
import org.springframework.data.ebean.domain.AbstractAggregateRoot;
2322

2423
import javax.persistence.*;
2524
import java.util.Arrays;
@@ -33,12 +32,11 @@
3332
*
3433
* @author Xuegui Yuan
3534
*/
36-
@AggregateRoot
3735
@Entity
3836
@Table(name = "user")
3937
@Getter
4038
@Setter
41-
public class User extends AbstractAuditableEntity {
39+
public class User extends AbstractAggregateRoot {
4240

4341
@Embedded
4442
private FullName fullName;

0 commit comments

Comments
 (0)