Spring Mysql Hibernate

0

Hej mam problem bo połączyłam tabele komentarzy z tabela postów i dostaje błąd

Hibernate: 
    select
        post0_.postid as postid1_7_,
        post0_.autor as autor2_7_,
        post0_.opis as opis3_7_,
        post0_.tytul as tytul4_7_ 
    from
        post post0_ 
    where
        post0_.postid=?
Hibernate: 
    select
        comment0_.idComment as idCommen1_5_,
        comment0_.autor as autor2_5_,
        comment0_.opis as opis3_5_,
        comment0_1_.idComment as idCommen1_6_ 
    from
        comment comment0_ 
    left outer join
        comment_post comment0_1_ 
            on comment0_.idComment=comment0_1_.postid
kwi 16, 2018 1:48:54 PM org.springframework.web.servlet.tags.form.HiddenInputTag doStartTag
SEVERE: Invalid property 'idComment' of bean class [java.util.ArrayList]: Bean property 'idComment' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
org.springframework.beans.NotReadablePropertyException: Invalid property 'idComment' of bean class [java.util.ArrayList]: Bean property 'idComment' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

w kontrolerze mam:

@RequestMapping(value="/posts/{postid}/comments")
@Controller
public class CommentController {

@RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView Comment(@PathVariable Long postid, ModelAndView model) {
		List<Post> list = postService.getAllPosts(postid);
        List<Comment> comment = commentService.getAllComment();
        model.addObject("comment", comment);
        model.addObject("list", list);
        model.setViewName("view");
        return model;
	
	}
	
	@RequestMapping(value = "/newComment", method = RequestMethod.GET)
    public ModelAndView newComment(@PathVariable Long postid, @ModelAttribute("comment")  Comment comment,
    		   BindingResult result){
		List<Post> list = postService.getAllPosts(postid);
		Map<String, Object> model = new HashMap<String, Object>();
        model.put("comment", commentService.getAllComment());
        model.put("list", list);
        return new ModelAndView("view", model);

    }
	
	@RequestMapping(value = "/saveComment", method = RequestMethod.POST)
	public ModelAndView saveComment(@PathVariable Long postid, @ModelAttribute Comment comment) {
		List<Post> list = postService.getAllPosts(postid);
		commentService.saveOrUpdate(comment);
	    return new ModelAndView("redirect:/posts/{postid}/comments");
	}

w .jsp

<div align="center">
        <form:form action="saveComment" method="post" commandName="comment">
        <form:hidden path="idComment"/>
        <div class="form-group row">
<div class="col-xs-4">
    <label for="ex3"></label>
    <input class="form-control" name="autor" id="ex3" type="string" placeholder="Autor">
  </div>
  </div>
  <div class="form-group row">
  <div class="col-xs-4">
    <label for="ex3"></label>
    <input class="form-control" name="opis" id="ex3" type="string" placeholder="Opis">
  </div>
</div>
<button type="submit" value="Save" class="btn btn-primary active">Dodaj</button>
        </form:form>
    </div>
    
<h2>${idComment}</h2>
<c:forEach var="post" items="${comment}">

<div class="container">
<div class="col-md-12">
    <h1>${comment.autor }</h1>
   
0

Bean property 'idComment' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

To powinno wszystko wyjaśnić. Po złączeniu tabel musisz również odwzorować to na modelach (tak działa ORM).

0

Pokaż kod klas encji, czyli Post i Comment.

0
@Entity
@Table(name = "comment")
public class Comment implements Serializable{

	private static final long serialVersionUID = 2984232256687388809L;
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	private int idComment;
	
	@Column
	private String autor;
	
	@Column
	private String opis;
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinTable(name="comment_post",
    joinColumns={@JoinColumn(name="postid")},
    inverseJoinColumns={@JoinColumn(name="idComment")})
	private Post post;

	public int getIdComment() {
		return idComment;
	}

	public void setIdComment(int idComment) {
		this.idComment = idComment;
	}

	public String getAutor() {
		return autor;
	}

	public void setAutor(String autor) {
		this.autor = autor;
	}

	public String getOpis() {
		return opis;
	}

	public void setOpis(String opis) {
		this.opis = opis;
	}

	public Post getPost() {
		return post;
	}

	public void setPost(Post post) {
		this.post = post;
	}
	
}

@Entity
@Table(name = "post")
public class Post implements Serializable{

	private static final long serialVersionUID = 8538629616831690171L;
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	private Long postid;
	
	@Column
	private String autor;
	
	@Column
	private String tytul;
	
	@Column
	private String opis;
	
	/*private Blob image;*/
	
	/*@OneToMany
	@JoinColumn(name="idComment")
	private List<Comment> comment;*/
	
	public String getAutor() {
		return autor;
	}

	/*public Blob getImage() {
		return image;
	}

	public void setImage(Blob image) {
		this.image = image;
	}*/

	public Long getPostid() {
		return postid;
	}

	public void setPostid(Long postid) {
		this.postid = postid;
	}

	public void setAutor(String autor) {
		this.autor = autor;
	}

	public String getTytul() {
		return tytul;
	}

	public void setTytul(String tytul) {
		this.tytul = tytul;
	}

	public String getOpis() {
		return opis;
	}

	public void setOpis(String opis) {
		this.opis = opis;
	}
	
	/*public List<Comment> getComment() {
		return comment;
	}

	public void setComment(List<Comment> comment) {
		this.comment = comment;
	}*/
	
	
}

0
<h2>${idComment}</h2>

próbujesz wywołać attrybut którego nie podałaś w controllerze.

<c:forEach var="post" items="${comment}">
<div class="container">
<div class="col-md-12">
   <h1>${comment.autor }</h1>

zamiast ${comment.autor } powinno być ${post.autor}

EDIT:

<form:hidden path="idComment"/>

Nie musisz przekazywać id, usuń tą linię i daj znać czy dalej występuje ten błąd.

0

Czy mogłabym prosić o to żeby ktoś spojrzał na to co zrobiłam bo nie moge sobie poradzić z wyświetleniem komentarzy odpowiadających postid?

PostController

@RequestMapping(value = "/{postid}", method = RequestMethod.GET)
    public ModelAndView findid(@PathVariable Long postid, ModelAndView model, Post post){
		List<Post> list = postService.getAllPosts(postid);
		List<Comment> comments = postService.get(postid).getComments();
        model.addObject("list", list);
        model.addObject("comments", comments);
        model.setViewName("view");
        return model;
    }

Post

@OneToMany(cascade = CascadeType.ALL, 
	        mappedBy = "post", orphanRemoval = true)
	@org.hibernate.annotations.LazyCollection(org.hibernate.annotations.LazyCollectionOption.EXTRA)
	/*@JoinColumn(name="idComment")*/
	private List<Comment> comments = new ArrayList<>();

public List<Comment> getComments() {
		return comments;
	}

	public void setComments(List<Comment> comments) {
		this.comments = comments;
	}
1

Powinnaś się odwołać do metody getComments() dla danego obiektu klasy Post.

Skoro masz PathVariable, aby uzyskać dostęp do postu z odpowiednim id, dlaczego przekazujesz do widoku listę wszystkich postów? powinno to wyglądać mniej więcej tak:

@RequestMapping(value = "/{postid}", method = RequestMethod.GET)
   public ModelAndView findid(@PathVariable Long postid, ModelAndView model, Post post){
       Post post = postService.gePost(postid);
       List<Comment> comments = post.getComments();
       model.addObject("post", post);
       model.addObject("comments", comments);
       model.setViewName("view");
       return model;
   }
0

Dzięki byłam blisko, pomogłeś mi ... :) Liste postów wyświetlam po id posta ponieważ chce żeby użytkownik widział pod jakim postem dodaje komentarz.. Teraz mam jeszcze jeden problem a natomiast chciałabym żebyś po raz ostatni wyjaśnił mi jedną rzecz... Czy ja aby na pewno dobrze połączyłam swoje tabele post i comment? Bo dostaje błąd a że to mój pierwszy projekt w tym frameworku to pewne rzeczy są jeszcze dla mnie nie jasne...

@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="postid", nullable = false, insertable = false, updatable = false)
	private Post post;

@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name="comment_post",
    joinColumns={@JoinColumn(name="postid")},
    inverseJoinColumns={@JoinColumn(name="idComment")})
	private List<Comment> comments = new ArrayList<>();

CREATE TABLE `post` (
  `postid` int(11) NOT NULL AUTO_INCREMENT,
  `autor` varchar(45) NOT NULL,
  `tytul` varchar(512) NOT NULL,
  `opis` varchar(5000) NOT NULL,
  PRIMARY KEY (`postid`)
);

CREATE TABLE `comment` (
  `idComment` int(11) NOT NULL AUTO_INCREMENT,
  `autor` varchar(45) NOT NULL,
  `opis` varchar(1000) NOT NULL,
  `postid` int(11) NOT NULL,
  PRIMARY KEY (`idComment`),
  KEY `fk_post` (`postid`),
  CONSTRAINT `fk_post` FOREIGN KEY (`postid`) REFERENCES `post` (`postid`)
);

CREATE TABLE comment_post (
    postid int(11) NOT NULL,
    idComment int(11) NOT NULL,
    PRIMARY KEY (postid),
    CONSTRAINT FK_comment FOREIGN KEY (idComment) REFERENCES comment (idComment)
);

Błąd który otrzymuje to brak pola postid ale przecież on powinien być wstawiany automatycznie czy nie?? :

Hibernate: 
    select
        comments0_.postid as postid1_7_0_,
        comments0_.idComment as idCommen2_6_0_,
        comment1_.idComment as idCommen1_5_1_,
        comment1_.autor as autor2_5_1_,
        comment1_.opis as opis3_5_1_,
        comment1_.postid as postid4_5_1_,
        post2_.postid as postid1_7_2_,
        post2_.autor as autor2_7_2_,
        post2_.opis as opis3_7_2_,
        post2_.tytul as tytul4_7_2_ 
    from
        comment_post comments0_ 
    inner join
        comment comment1_ 
            on comments0_.idComment=comment1_.idComment 
    inner join
        post post2_ 
            on comment1_.postid=post2_.postid 
    where
        comments0_.postid=?
Hibernate: 
    select
        post0_.postid as postid1_7_0_,
        post0_.autor as autor2_7_0_,
        post0_.opis as opis3_7_0_,
        post0_.tytul as tytul4_7_0_,
        comments1_.postid as postid1_7_1_,
        comment2_.idComment as idCommen2_6_1_,
        comment2_.idComment as idCommen1_5_2_,
        comment2_.autor as autor2_5_2_,
        comment2_.opis as opis3_5_2_,
        comment2_.postid as postid4_5_2_,
        post3_.postid as postid1_7_3_,
        post3_.autor as autor2_7_3_,
        post3_.opis as opis3_7_3_,
        post3_.tytul as tytul4_7_3_ 
    from
        post post0_ 
    left outer join
        comment_post comments1_ 
            on post0_.postid=comments1_.postid 
    left outer join
        comment comment2_ 
            on comments1_.idComment=comment2_.idComment 
    left outer join
        post post3_ 
            on comment2_.postid=post3_.postid 
    where
        post0_.postid=?
Hibernate: 
    insert 
    into
        comment
        (autor, opis) 
    values
        (?, ?)
19:20:55.355 [http-bio-8088-exec-4] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Error: 1364, SQLState: HY000
19:20:55.356 [http-bio-8088-exec-4] ERROR o.h.e.jdbc.spi.SqlExceptionHelper - Field 'postid' doesn't have a default value
19:20:55.358 [http-bio-8088-exec-4] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - SQL Warning Code: 1364, SQLState: HY000
19:20:55.358 [http-bio-8088-exec-4] WARN  o.h.e.jdbc.spi.SqlExceptionHelper - Field 'postid' doesn't have a default value
0

masz tu klasyczny związek jeden-do-wielu, trochę przekombinowałaś z mapowaniem - zobacz jak dokładnie powinnaś to zrobić:
https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

1 użytkowników online, w tym zalogowanych: 0, gości: 1