Guest User

Untitled

a guest
Oct 18th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. @SpringBootApplication
  2. public class SocialMediaApplication {
  3.  
  4. public static void main(String[] args) {
  5. SpringApplication.run(SocialMediaApplication.class, args);
  6. }
  7. }
  8.  
  9. @Entity
  10. public class Post implements Serializable {
  11.  
  12. private static final long serialVersionUID = 1L;
  13.  
  14. @Id
  15. @GeneratedValue(strategy = GenerationType.AUTO)
  16. private Long id;
  17.  
  18. @Column(nullable = false)
  19. private String content;
  20.  
  21. @ManyToOne
  22. @JoinColumn(name = "user_id")
  23. private User user;
  24.  
  25. @Column(nullable = false)
  26. private Timestamp createdAt;
  27.  
  28. @Column
  29. private String location;
  30.  
  31. @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
  32. private List<Attachment> attachmentList;
  33.  
  34. @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
  35. private List<Rating> ratingList;
  36.  
  37. public Post() {
  38. }
  39.  
  40. public Post(String content, User user, Timestamp createdAt, String location, List<Attachment> attachmentList, List<Rating> ratingList) {
  41. super();
  42. this.content = content;
  43. this.user = user;
  44. this.createdAt = createdAt;
  45. this.location = location;
  46. this.attachmentList = attachmentList;
  47. this.ratingList = ratingList;
  48. }
  49.  
  50. // ...
  51. }
  52.  
  53. @Entity
  54. public class Attachment implements Serializable {
  55.  
  56. private static final long serialVersionUID = 1L;
  57.  
  58. @Id
  59. @GeneratedValue(strategy = GenerationType.AUTO)
  60. private Long id;
  61.  
  62. @Lob
  63. @Column(length = 100_000, nullable = false)
  64. private byte[] content;
  65.  
  66. @ManyToOne
  67. @JoinColumn(name = "post_id")
  68. private Post post;
  69.  
  70. public Attachment() {
  71. }
  72.  
  73. public Attachment(byte[] content, Post post) {
  74. super();
  75. this.content = content;
  76. this.post = post;
  77. }
  78.  
  79. // ...
  80. }
  81.  
  82. @Entity
  83. public class User implements Serializable {
  84.  
  85. private static final long serialVersionUID = 1L;
  86.  
  87. @Id
  88. @GeneratedValue
  89. private Long id;
  90.  
  91. @Column(nullable = false)
  92. private String firstName;
  93.  
  94. @Column(nullable = false)
  95. private String lastName;
  96.  
  97. @Column(nullable = false)
  98. private Date dateOfBirth;
  99.  
  100. @Column(nullable = false)
  101. private String credential;
  102.  
  103. @Column(nullable = false)
  104. private String password;
  105.  
  106. @Column
  107. private String location;
  108.  
  109. @Lob
  110. @Column(length = 100_000)
  111. private byte[] photo;
  112.  
  113. @Column
  114. private String motto;
  115.  
  116. public User() {
  117. }
  118.  
  119. public User(String firstName, String lastName, Date dateOfBirth, String credential, String password,
  120. String location, byte[] photo, String motto) {
  121. super();
  122. this.firstName = firstName;
  123. this.lastName = lastName;
  124. this.dateOfBirth = dateOfBirth;
  125. this.credential = credential;
  126. this.password = password;
  127. this.location = location;
  128. this.photo = photo;
  129. this.motto = motto;
  130. }
  131.  
  132. // ...
  133. }
  134.  
  135. @Transactional
  136. public interface PostRepository extends CrudRepository<Post, Long> {
  137.  
  138. }
  139.  
  140. @Transactional
  141. public interface AttachmentRepository extends CrudRepository<Attachment, Long> {
  142.  
  143. }
  144.  
  145. @Transactional
  146. public interface UserRepository extends CrudRepository<User, Long> {
  147.  
  148. }
  149.  
  150. @Controller
  151. @RequestMapping("/post")
  152. public class PostController {
  153.  
  154. @Autowired
  155. PostRepository postRepository;
  156.  
  157. @GetMapping("/add")
  158. public String greetingForm(Model model) {
  159. model.addAttribute("post", new Post());
  160. return "addPost";
  161. }
  162.  
  163. @PostMapping("/add")
  164. public String addPost(@ModelAttribute Post post, @RequestParam("attachment") MultipartFile uploadingFile) throws IOException {
  165. User user = new User();
  166. user.setId(1L);
  167. post.setUser(user);
  168. post.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
  169. List<Attachment> attachmentList = new ArrayList<>();
  170. Attachment attachment = new Attachment();
  171. attachment.setContent(uploadingFile.getBytes());
  172. attachment.setPost(post);
  173. attachmentList.add(attachment);
  174. post.setAttachmentList(attachmentList);
  175. List<Rating> ratingList = new ArrayList<>();
  176. post.setRatingList(ratingList);
  177. postRepository.save(post);
  178. return "allPosts";
  179. }
  180.  
  181. }
  182.  
  183. spring.datasource.url=jdbc:mysql://localhost:3306/****?useSSL=false
  184. spring.datasource.username=root
  185. spring.datasource.password=****
  186. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  187. spring.jpa.properties.hibernate.id.new_generator_mappings=false
  188. spring.jpa.show-sql=true
  189.  
  190. spring.thymeleaf.cache=false
  191. spring.thymeleaf.enabled=true
  192. spring.thymeleaf.prefix=classpath:/templates/
  193. spring.thymeleaf.suffix=.html
  194.  
  195. Hibernate: insert into post (content, created_at, location, user_id) values (?, ?, ?, ?)
  196. Hibernate: insert into attachment (content, post_id) values (?, ?)
Add Comment
Please, Sign In to add comment