-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: ManyToMany or else method of JoinTable with Spring Data
PostPosted: Sun Feb 05, 2017 7:53 am 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
How I can to edit these tables?

I.e. I have entities Category and Post
in Post, I have:
Code:
@ManyToMany(fetch = FetchType.EAGER, cascade = { CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE })
@JoinTable(name = "category_post", joinColumns = @JoinColumn(name = "categoryId"), inverseJoinColumns = @JoinColumn(name = "postId"))
private Set<Category> categories = new HashSet<>();


in Category, I have:
Code:
@ManyToMany(fetch = FetchType.EAGER, mappedBy = "categories")
private Set<Post> posts;


I want to edit categories in the table of 'category_post'
So, I try to use custom 'update' method for Post entity

Code:
@Modifying
@Query("update ru.mrchebik.model.Post post set post.title = :title, post.text = :text, post.categories = :categories where post.postId = :postId")
void updatePost(@Param("postId") long id, @Param("title") String title, @Param("text") String text, @Param("categories") Set<Category> categories);


But also I get exception. No matter what, because I realize I have no right trying to do it.

There, I try to edit categories from Post class:
Code:
    @RequestMapping(value = "{id}/edit", method = POST)
    public String saveEdit(@PathVariable String id,
                           @RequestParam String categoriesId,
                           @RequestParam String title,
                           @RequestParam String text) {
        if (categoriesId.equals("")) {
            Post post = postService.findPost(Long.parseLong(id));
            post.getCategory().clear();
            post.setTitle(title);
            post.setText(text);
            postService.update(post);
        } else {
            Post post = postService.findPost(Long.parseLong(id));
            post.getCategory().clear();
            for (String category : categoriesId.split(",")) {
                post.getCategory().add(categoryService.findById(Long.parseLong(category)));
            }
            post.setTitle(title);
            post.setText(text);
            postService.update(post);
        }

        return "redirect:/blog/";
    }


Top
 Profile  
 
 Post subject: Re: ManyToMany or else method of JoinTable with Spring Data
PostPosted: Sun Feb 05, 2017 1:17 pm 
Newbie

Joined: Sat Nov 26, 2016 3:20 am
Posts: 10
I tried to use saveAndFlush and post was updated. Problem resolved.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.