-
Notifications
You must be signed in to change notification settings - Fork 6
/
SpringDataJdbcManyToManyApplication.java
178 lines (151 loc) · 5.9 KB
/
SpringDataJdbcManyToManyApplication.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.example.springdatajdbconetomany;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.PersistenceConstructor;
import org.springframework.data.annotation.Transient;
import org.springframework.data.domain.AbstractAggregateRoot;
import org.springframework.data.domain.Persistable;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.repository.CrudRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;
@With
@Value
@Table("message_author")
class AuthorRef {
// id of author that the message is referencing on...
// should be same as in SQL message_author definition...
private final UUID author;
}
/**
* {@link Author} will be part of many {@link Message} objects.
* see {@link AuthorRef} as a many(authors)-to-many(messages)
* relationship implementation.
*/
@With
@Value
@EqualsAndHashCode(callSuper = false)
class Author extends AbstractAggregateRoot<Author> implements Persistable<UUID> {
final @Id UUID id;
final String name, email;
final LocalDateTime lastModifiedDateTime;
@Override
@Transient
@JsonIgnore
public boolean isNew() {
Optional<UUID> maybeId = Optional.ofNullable(this.id);
return !maybeId.isPresent() || maybeId.map(UUID::toString)
.filter(id -> id.startsWith("00000000-0000-0000-0000-00000000000"))
.isPresent();
}
}
interface AuthorRepository extends CrudRepository<Author, UUID> {
@Query(" SELECT * FROM author ORDER BY last_modified_date_time DESC ; ")
Iterable<Author> findAllByOrderByLastModifiedDateTimeDesc();
}
@RestController
@RequiredArgsConstructor
@RequestMapping("/authors")
class AuthorResource {
private final AuthorRepository authorRepository;
@GetMapping("/{id}")
ResponseEntity<Author> findById(@PathVariable UUID id) {
return authorRepository.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound()
.build());
}
@GetMapping
Iterable<Author> findAll() {
return authorRepository.findAllByOrderByLastModifiedDateTimeDesc();
}
@PostMapping
@Transactional
ResponseEntity<Author> save(@RequestBody Author author) {
Author entity = authorRepository.save(author);
UUID id = Objects.requireNonNull(entity.getId());
return authorRepository.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound()
.build());
}
}
/********/
@With
@Value
@EqualsAndHashCode(callSuper = false)
class Message extends AbstractAggregateRoot<Message> implements Persistable<UUID> {
final @Id @Getter UUID id;
final UUID aggregateId;
final String body;
final LocalDateTime lastModifiedDateTime;
final Set<AuthorRef> authors;
@PersistenceConstructor
Message(UUID id, UUID aggregateId, String body, LocalDateTime lastModifiedDateTime, Set<AuthorRef> authors) {
this.id = id;
this.aggregateId = aggregateId;
this.body = body;
this.lastModifiedDateTime = lastModifiedDateTime;
this.authors = Optional.ofNullable(authors).orElse(new HashSet<>());
}
public Message withAuthor(Author... authors) {
this.authors.addAll(Arrays.stream(authors)
.map(Author::getId)
.map(AuthorRef::new).collect(Collectors.toList()));
return this;
}
@Override
@Transient
@JsonIgnore
public boolean isNew() {
Optional<UUID> maybeId = Optional.ofNullable(this.id);
return !maybeId.isPresent() || maybeId.map(UUID::toString)
.filter(id -> id.startsWith("00000000-0000-0000-0000-00000000000"))
.isPresent();
}
}
interface MessagesRepository extends CrudRepository<Message, UUID> {
@Query(" SELECT * FROM message ORDER BY last_modified_date_time DESC ; ")
Collection<Message> findAllByOrderByLastModifiedDateTimeDesc();
}
@RestController
@RequiredArgsConstructor
@RequestMapping("/messages")
class MessageResource {
private final MessagesRepository messagesRepository;
@GetMapping
Iterable<Message> findAll() {
return messagesRepository.findAllByOrderByLastModifiedDateTimeDesc();
}
@GetMapping("{id}")
ResponseEntity<Message> findById(@PathVariable UUID id) {
Optional<Message> result = messagesRepository.findById(id);
return result.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound()
.build());
}
@PostMapping
ResponseEntity<Message> save(@RequestBody Message message) {
Message entity = messagesRepository.save(message);
UUID id = Objects.requireNonNull(entity.getId());
return messagesRepository.findById(id)
.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound()
.build());
}
}
/********/
@SpringBootApplication
public class SpringDataJdbcManyToManyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringDataJdbcManyToManyApplication.class, args);
}
}