Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- public class Users {
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- private Long id;
- @Column(nullable=false, length=30)
- private String username;
- @Column(nullable=false)
- private String hashPass;
- // Unique email
- @Column(nullable=false, unique=true)
- private String email;
- @Column(nullable=false)
- private String role;
- @OneToMany(mappedBy="author", cascade=CascadeType.ALL)
- private Set<Post> posts= new HashSet<>();
- public Users(){
- }
- public Users(String username){
- this.username=username;
- }
- public Users(String username, String hashPass, String email, String role) {
- super();
- this.username = username;
- this.hashPass = hashPass;
- this.email = email;
- this.role = role;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getHashPass() {
- return hashPass;
- }
- public void setHashPass(String hashPass) {
- this.hashPass = hashPass;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public Set<Post> getPosts() {
- return posts;
- }
- public void setPosts(Set<Post> posts) {
- this.posts = posts;
- }
- public String getRole() {
- return role;
- }
- public void setRole(String role) {
- this.role = role;
- }
- @Override
- public String toString() {
- return "Users [id=" + id + ", username=" + username + ", hashPass=" + hashPass + ", email=" + email + ", role="
- + role + ", posts=" + posts + "]";
- }
- }
- @Entity
- public class Post {
- @Id
- @GeneratedValue(strategy=GenerationType.AUTO)
- private Long id;
- @Column(nullable=false, length=300)
- private String title;
- @Column(nullable=false)
- private String body;
- @ManyToOne(optional=false, fetch=FetchType.LAZY)
- @JoinColumn
- private Users author;
- @Column(nullable=false)
- private Date date= new Date();
- public Post(){
- }
- public Post(String title, String body, Users author){
- super();
- this.title=title;
- this.body=body;
- this.author=author;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- public String getBody() {
- return body;
- }
- public void setBody(String body) {
- this.body = body;
- }
- public Users getAuthor() {
- return author;
- }
- public void setAuthor(Users author) {
- this.author = author;
- }
- public Date getDate() {
- return date;
- }
- public void setDate(Date date) {
- this.date = date;
- }
- @Override
- public String toString() {
- return "Post [id=" + id + ", title=" + title + ", body=" + body + ", author=" + author + ", date=" + date + "]";
- }
- }
- // POST CONTROLLER
- //Add Posts
- @RequestMapping(value="/posts/create", method=RequestMethod.GET)
- public String addPost(String username, Model model){
- model.addAttribute("add", new Post());
- model.addAttribute("author", uRepo.findByUsername(username));
- return "posts/create";
- }
- //Save post
- @RequestMapping(value="/posts/savePost", method=RequestMethod.POST)
- public String savePost(Post post){
- pRepo.save(post);
- return "redirect:/";
- }
- <div class="container">
- <h1>Add Post</h1>
- <form th:object="${add}" th:action="@{savePost}" action="#" method="post" class="form-horizontal">
- <div class="form-group">
- <label class="control-label col-sm-2" for="title">Title</label>
- <div class="col-sm-10"><input type="text" id="title" th:field="*{title}" /></div>
- </div>
- <div class="form-group">
- <label class="control-label col-sm-2" for="body">Body:</label>
- <div class="col-sm-10">
- <textarea rows="10" id="body" th:field="*{body}"></textarea>
- </div>
- </div>
- <div class="form-group">
- <label class="control-label col-sm-2" for="author">Author:</label>
- <div class="col-sm-10">
- <p th:inline="text" id="author" th:field="*{author}">[[${#httpServletRequest.remoteUser}]] <span th:sec:authentication="name"></span></p>
- </div>
- </div>
- <br/>
- <input type="submit" value="Save" class="btn btn-info"></input>
- </form>
- </div>
Add Comment
Please, Sign In to add comment