我有以下模型:文章可以有一些标签,标签可以在某些文章上。 因此,这是与3个表的多对多关系:
当我删除标签时,我想删除:
但我当然不想删除文章中的文章。
我该怎么做?
我试试这个,但它不起作用:
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 */
}
答案 0 :(得分:0)
找到解决方案。在删除时,我们需要确保不将删除级联到文章,反之亦然。
@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}
添加到您的代码实体,以防止删除代码以删除其相关文章。
答案 1 :(得分: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 !!!";
}