Guest User

Untitled

a guest
Feb 13th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. @Entity
  2. @Table(name = "users")
  3. public 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")
  73. public 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
  141. public interface DocumentRepository extends CrudRepository<Document, Integer> {
  142.  
  143. }
  144.  
  145. @Repository
  146. public interface UserRepository extends CrudRepository<User, Integer> {
  147.  
  148. }
  149.  
  150. spring.jpa.hibernate.ddl-auto=validate
  151. spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  152. spring.jpa.show-sql=true
  153.  
  154. spring.datasource.url=jdbc:mysql://localhost:3306/many_to_many?useSSL=false
  155. spring.datasource.username=root
  156. spring.datasource.password=root
  157. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
  158.  
  159. drop database if exists many_to_many;
  160. create database many_to_many;
  161. use many_to_many;
  162.  
  163. create table users (
  164. id int primary key auto_increment,
  165. `name` varchar(255) not null unique
  166. );
  167.  
  168. create 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
  178. public 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.  
  217. Document {
  218. name: document1,
  219. author: user1,
  220. concurrent: user2,
  221. }
  222. Document {
  223. name: document2,
  224. author: user1,
  225. concurrent: user3,
  226. }
Add Comment
Please, Sign In to add comment