package net.roseindia.dao;
import java.util.Date;
import java.util.List;
import net.roseindia.model.Article;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
@Repository("articleDao")
public class ArticleDaoImpl implements ArticleDao {
@Autowired
private SessionFactory sessionFactory;
// To Save the article detail
public void saveArticle(Article article) {
article.setAddedDate(new Date());
sessionFactory.getCurrentSession().saveOrUpdate(article);
}
public void deleteArticle(Integer articleId){
System.out.println("Dao-->"+articleId);//能够打印出id号
Article article = (Article) sessionFactory.getCurrentSession().load(
Article.class,articleId);
sessionFactory.getCurrentSession().beginTransaction();
if (null != article) {
sessionFactory.getCurrentSession().delete(article);
}
}
// To get list of all articles
@SuppressWarnings("unchecked")
public List<Article> listArticles() {
return (List<Article>) sessionFactory.getCurrentSession().createCriteria(Article.class).list();
}
}
Dao-->3
Hibernate: select article0_.article_id as article1_0_0_, article0_.date_added as date2_0_0_, article0_.article_desc as article3_0_0_, article0_.article_name as article4_0_0_ from article article0_ where article0_.article_id=?
Hibernate: select this_.article_id as article1_0_0_, this_.date_added as date2_0_0_, this_.article_desc as article3_0_0_, this_.article_name as article4_0_0_ from article this_
为啥就是无法删除?
Article.java
package net.roseindia.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name = "article")
public class Article {
@Id
@GeneratedValue
@Column(name = "article_id")
private Integer articleId;
@Column(name = "article_name", nullable = false, length=20)
private String articleName;
@Column(name = "article_desc", nullable = false)
private String articleDesc;
@Column(name = "date_added")
private Date addedDate;
public Article() {
}
public Integer getArticleId() {
return articleId;
}
public void setArticleId(Integer articleId) {
this.articleId = articleId;
}
public String getArticleName() {
return articleName;
}
public void setArticleName(String articleName) {
this.articleName = articleName;
}
public String getArticleDesc() {
return articleDesc;
}
public void setArticleDesc(String articleDesc) {
this.articleDesc = articleDesc;
}
public Date getAddedDate() {
return addedDate;
}
public void setAddedDate(Date addedDate) {
this.addedDate = addedDate;
}
}
求教,谢谢了~
------解决方案--------------------
事物操作要commit的。
------解决方案--------------------