8

我有下面的模型:一篇文章可以有一些标签,一个标签可以在一些文章上。所以这是与 3 个表的多对多关系:

  • 文章
  • ARTICLE_TAG
  • 标签

当我删除标签时,我想删除:

  • TAG中的标签
  • 标签与在 ARTICLE_TAG 中标记的文章之间的所有关系

但我当然不想删除 ARTICLE 中的文章。

我怎样才能做到这一点 ?

我试试这个,但它不起作用:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
for (Article article : tagToDelete.getArticles()) {
    article.getTags().remove(tagToDelete);
}
session.delete(tagToDelete);

谢谢 !

@Entity
@Table(name="ARTICLE")
public class Article extends AbstractAuditedEntity {

    @Id
    @Column(name="ID", nullable=false)
    private Long id;

    @ManyToMany
    @JoinTable(name="ARTICLE_TAG", joinColumns=@JoinColumn(name = "ARTICLE_ID"), inverseJoinColumns=@JoinColumn(name = "TAG_ID"))
    private Set<Tag> tags = new HashSet<>();

    public Article() {}

    /** Getters & Setters */
}

@Entity
@Table(name="TAG")
public class Tag {

    @Id
    @Column(name="ID", nullable=false)
    private Long id;    

    @ManyToMany(mappedBy="tags")
    private Set<Article> articles = new HashSet<>();

    public Tag() {}

    /** Getters & Setters */        
}
4

2 回答 2

1

找到了解决方案。在删除时,我们需要确保不要将删除级联到文章,反之亦然。

@ManyToMany(cascade={PERSIST, DETACH})
@JoinTable(name="ARTICLE_TAG",
           joinColumns=@JoinColumn(name = "ARTICLE_ID"), 
           inverseJoinColumns=@JoinColumn(name = "TAG_ID"))
private Set<Tag> tags = new HashSet<>();

我的问题是使用CascadeType.All,默认情况下包括CascadeType.REMOVE,它将删除一篇文章到它包含的标签。

您还可以添加cascade={PERSIST, DETACH}到您的标签实体以防止删除标签以删除其关联的文章。

于 2017-05-16T17:47:40.810 回答
0

我尝试删除这样的记录。有效。

传递id给 API,然后它会删除每条记录。试试看。

有任何问题请告诉我。

@Entity
@Table(name="ARTICLE") 
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "article_id")
    private int id;

    @ManyToMany(mappedBy="article")
    private Set<Article_Tag> article_tag = new HashSet<>();

    public Article() {}

    /** Getters & Setters */



@Entity
@Table(name="TAG")
public class Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "tag_id")
    private int id;

    @ManyToMany(mappedBy="tag")
    private Set<Article_Tag> article_tag = new HashSet<>();

    public Tag() {}

    /** Getters & Setters */



@Entity
@Table(name="ARTICLE_TAG")
public class Article_Tag {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.DETACH})
    @joinColumn(name = "article_id")
    private Set<Article> articles = new HashSet<>();

    @ManyToMany(cascade={CascadeType.PERSIST, CascadeType.DETACH})
    @joinColumn(name = "tag_id")
    private Set<Tag> tags = new HashSet<>();

    public Article_Tag() {}

    /** Getters & Setters */



public interface Article_TagRepository extends JpaRepository<Article_Tag, Integer>{

    Article_Tag findByArticle(Article id);

    Article_Tag findByTag(Tag id);

}



@RestController
@RequestMapping(value = "/")
public class Article_TagController {

@Autowired
private Article_TagRepository article_tagRepository;

@GetMapping("/delete/article/{id}")
public String DeleteArticleById(@PathVariable("id") Article id) {
    Article_Tag article_tag = article_tagRepository.findByArticle(id);
    Integer article_tag_id = article_tag.getId();
    article_tagRepository.deleteById(article_tag_id);
    return "Article Successfully Deleted !!!";
}

@GetMapping("/delete/tag/{id}")
public String DeleteTagById(@PathVariable("id") Tag id) {
    Article_Tag article_tag = article_tagRepository.findByTag(id);
    Integer article_tag_id = article_tag.getId();
    article_tagRepository.deleteById(article_tag_id);
    return "Tag Successfully Deleted !!!";
}
于 2019-09-25T07:30:39.617 回答