· 7 years ago · Feb 13, 2019, 08:40 AM
1@Entity
2@Table(name = "users")
3public class User implements Serializable {
4
5 @Id
6 @GeneratedValue(strategy = GenerationType.IDENTITY)
7 private int id;
8
9 @Column(name = "`name`", nullable = false, unique = true)
10 private String name;
11
12 @OneToMany(fetch = FetchType.EAGER, mappedBy = "author")
13 private Set<Document> authorDocuments = new HashSet<>();
14
15 @OneToMany(fetch = FetchType.EAGER, mappedBy = "concurrent")
16 private Set<Document> concurrentDocuments = new HashSet<>();
17
18 public User() {
19 }
20
21 public User(String name) {
22 this.name = name;
23 }
24
25 public int getId() {
26 return id;
27 }
28
29 public void setId(int id) {
30 this.id = id;
31 }
32
33 public String getName() {
34 return name;
35 }
36
37 public void setName(String name) {
38 this.name = name;
39 }
40
41 public Set<Document> getAuthorDocuments() {
42 return authorDocuments;
43 }
44
45 public Set<Document> getConcurrentDocuments() {
46 return concurrentDocuments;
47 }
48
49 public void addDocumentAuthor(Document document) {
50 authorDocuments.add(document);
51 document.setAuthor(this);
52 }
53
54 public void removeDocumentAuthor(Document document) {
55 authorDocuments.remove(document);
56 document.setAuthor(null);
57 }
58
59 public void addDocumentConcurrent(Document document) {
60 concurrentDocuments.add(document);
61 document.setConcurrent(this);
62 }
63
64 public void removeDocumentConcurrent(Document document) {
65 concurrentDocuments.remove(document);
66 document.setConcurrent(null);
67 }
68
69}
70
71@Entity
72@Table(name = "documents")
73public class Document implements Serializable {
74
75 @Id
76 @GeneratedValue(strategy = GenerationType.IDENTITY)
77 private int id;
78
79 @Column(name = "`name`", nullable = false, unique = true)
80 private String name;
81
82 @ManyToOne(fetch = FetchType.EAGER)
83 @JoinColumn(name = "id_author", nullable = false)
84 private User author;
85
86 @ManyToOne(fetch = FetchType.EAGER)
87 @JoinColumn(name = "id_concurrent", nullable = false)
88 private User concurrent;
89
90 public Document() {
91 }
92
93 public Document(String name) {
94 this.name = name;
95 }
96
97 public int getId() {
98 return id;
99 }
100
101 public void setId(int id) {
102 this.id = id;
103 }
104
105 public String getName() {
106 return name;
107 }
108
109 public void setName(String name) {
110 this.name = name;
111 }
112
113 public User getAuthor() {
114 return author;
115 }
116
117 public void setAuthor(User author) {
118 this.author = author;
119 }
120
121 public User getConcurrent() {
122 return concurrent;
123 }
124
125 public void setConcurrent(User concurrent) {
126 this.concurrent = concurrent;
127 }
128
129 @Override
130 public String toString() {
131 return "Document {" + lineSeparator()
132 + "name: " + name + "," + lineSeparator()
133 + "author: " + author.getName() + "," + lineSeparator()
134 + "concurrent: " + concurrent.getName() + lineSeparator()
135 + "}";
136 }
137
138}
139
140@Repository
141public interface DocumentRepository extends CrudRepository<Document, Integer> {
142
143}
144
145@Repository
146public interface UserRepository extends CrudRepository<User, Integer> {
147
148}
149
150spring.jpa.hibernate.ddl-auto=validate
151spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
152spring.jpa.show-sql=true
153
154spring.datasource.url=jdbc:mysql://localhost:3306/many_to_many?useSSL=false
155spring.datasource.username=root
156spring.datasource.password=root
157spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
158
159drop database if exists many_to_many;
160create database many_to_many;
161use many_to_many;
162
163create table users (
164 id int primary key auto_increment,
165 `name` varchar(255) not null unique
166);
167
168create table documents (
169 id int primary key auto_increment,
170 `name` varchar(255) not null unique,
171 id_author int not null,
172 id_concurrent int not null,
173 foreign key(id_author) references users(id),
174 foreign key(id_concurrent) references users(id)
175);
176
177@SpringBootApplication
178public class Application implements CommandLineRunner {
179
180 @Autowired
181 private UserRepository userRepository;
182
183 @Autowired
184 private DocumentRepository documentRepository;
185
186 public static void main(String[] args) {
187 SpringApplication.run(Application.class, args);
188 }
189
190 @Override
191 public void run(String... args) throws Exception {
192 User user1 = new User("user1");
193 User user2 = new User("user2");
194 User user3 = new User("user3");
195
196 userRepository.save(user1);
197 userRepository.save(user2);
198 userRepository.save(user3);
199
200 Document document1 = new Document("document1");
201 Document document2 = new Document("document2");
202
203 user1.addDocumentAuthor(document1);
204 user2.addDocumentConcurrent(document1);
205
206 user1.addDocumentAuthor(document2);
207 user3.addDocumentConcurrent(document2);
208
209 documentRepository.save(document1);
210 documentRepository.save(document2);
211
212 documentRepository.findAll().forEach(System.out::println);
213 }
214
215}
216
217Document {
218name: document1,
219author: user1,
220concurrent: user2,
221}
222Document {
223name: document2,
224author: user1,
225concurrent: user3,
226}