Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SpringBootApplication
- public class SocialMediaApplication {
- public static void main(String[] args) {
- SpringApplication.run(SocialMediaApplication.class, args);
- }
- }
- @Entity
- public class Post implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @Column(nullable = false)
- private String content;
- @ManyToOne
- @JoinColumn(name = "user_id")
- private User user;
- @Column(nullable = false)
- private Timestamp createdAt;
- @Column
- private String location;
- @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
- private List<Attachment> attachmentList;
- @OneToMany(mappedBy = "post", cascade = CascadeType.ALL)
- private List<Rating> ratingList;
- public Post() {
- }
- public Post(String content, User user, Timestamp createdAt, String location, List<Attachment> attachmentList, List<Rating> ratingList) {
- super();
- this.content = content;
- this.user = user;
- this.createdAt = createdAt;
- this.location = location;
- this.attachmentList = attachmentList;
- this.ratingList = ratingList;
- }
- // ...
- }
- @Entity
- public class Attachment implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private Long id;
- @Lob
- @Column(length = 100_000, nullable = false)
- private byte[] content;
- @ManyToOne
- @JoinColumn(name = "post_id")
- private Post post;
- public Attachment() {
- }
- public Attachment(byte[] content, Post post) {
- super();
- this.content = content;
- this.post = post;
- }
- // ...
- }
- @Entity
- public class User implements Serializable {
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue
- private Long id;
- @Column(nullable = false)
- private String firstName;
- @Column(nullable = false)
- private String lastName;
- @Column(nullable = false)
- private Date dateOfBirth;
- @Column(nullable = false)
- private String credential;
- @Column(nullable = false)
- private String password;
- @Column
- private String location;
- @Lob
- @Column(length = 100_000)
- private byte[] photo;
- @Column
- private String motto;
- public User() {
- }
- public User(String firstName, String lastName, Date dateOfBirth, String credential, String password,
- String location, byte[] photo, String motto) {
- super();
- this.firstName = firstName;
- this.lastName = lastName;
- this.dateOfBirth = dateOfBirth;
- this.credential = credential;
- this.password = password;
- this.location = location;
- this.photo = photo;
- this.motto = motto;
- }
- // ...
- }
- @Transactional
- public interface PostRepository extends CrudRepository<Post, Long> {
- }
- @Transactional
- public interface AttachmentRepository extends CrudRepository<Attachment, Long> {
- }
- @Transactional
- public interface UserRepository extends CrudRepository<User, Long> {
- }
- @Controller
- @RequestMapping("/post")
- public class PostController {
- @Autowired
- PostRepository postRepository;
- @GetMapping("/add")
- public String greetingForm(Model model) {
- model.addAttribute("post", new Post());
- return "addPost";
- }
- @PostMapping("/add")
- public String addPost(@ModelAttribute Post post, @RequestParam("attachment") MultipartFile uploadingFile) throws IOException {
- User user = new User();
- user.setId(1L);
- post.setUser(user);
- post.setCreatedAt(Timestamp.valueOf(LocalDateTime.now()));
- List<Attachment> attachmentList = new ArrayList<>();
- Attachment attachment = new Attachment();
- attachment.setContent(uploadingFile.getBytes());
- attachment.setPost(post);
- attachmentList.add(attachment);
- post.setAttachmentList(attachmentList);
- List<Rating> ratingList = new ArrayList<>();
- post.setRatingList(ratingList);
- postRepository.save(post);
- return "allPosts";
- }
- }
- spring.datasource.url=jdbc:mysql://localhost:3306/****?useSSL=false
- spring.datasource.username=root
- spring.datasource.password=****
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- spring.jpa.properties.hibernate.id.new_generator_mappings=false
- spring.jpa.show-sql=true
- spring.thymeleaf.cache=false
- spring.thymeleaf.enabled=true
- spring.thymeleaf.prefix=classpath:/templates/
- spring.thymeleaf.suffix=.html
- Hibernate: insert into post (content, created_at, location, user_id) values (?, ?, ?, ?)
- Hibernate: insert into attachment (content, post_id) values (?, ?)
Add Comment
Please, Sign In to add comment