一对多:
Article(文章类型)
@OneToMany(mappedBy = "article", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@OrderBy(value = "insertDate")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
private List<ArticleItem> articleItems = new ArrayList<ArticleItem>();
/*
* 添加文章
*/
public void addArticleItem(ArticleItem articleitem) {
if (!this.articleItems.contains(articleitem)) {
this.articleItems.add(articleitem);
articleitem.setArticle(this);
}
}
ArticleItem(文章)
@ManyToOne(cascade = CascadeType.REFRESH, optional = false)
@JoinColumn(name = "article_id")
private Article article;
删除一篇文章
delete(ArticleItem.class,articleItemId)
删除某一类型文章,文章类型不删除
String hql = "delete from ArticleItem where article.id=?";
generalDao.executeUpdate(hql,articleid);
删除文章类型,对应文章全部删除
delete(Article.class, articleid)
修改一篇文章
articleitem.setArticle(article);
generalDao.saveOrUpdate(articleitem);
添加一篇文章
article.addArticleItem(articleitem);
generalDao.saveOrUpdate(article);
添加文章类型
article.setArticleItems(new ArrayList<ArticleItem>());
generalDao.saveOrUpdate(article);
获得所有类型文章及文章总数
String hql = "select o.id,o.articletype,count(items) from Article o left outer join o.articleItems as items group by o.id,o.articletype";
page = generalDao.findForPage(page, hql);
获得所有文章
String hql = "from ArticleItem";
page = generalDao.find(page, hql);