hibernate嵌套条件为空结果

时间:2013-06-02 12:17:57

标签: java hibernate

问题是当我得到一些元素时,我在查询上得到空结果。这是代码:

DetachedCriteria criteria = DetachedCriteria.forClass(Article.class);
DetachedCriteria authorCriteria = criteria.createCriteria("author");
authorCriteria.add(Restrictions.eq("id",((User)session.getAttribute("user")).getId()));
List<Article> articles = articleManager.findArticleByCriteria(criteria);


@Entity
@Table(name = "ARTICLES")
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    Integer id;
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "author_fk")
    Writer author;
    @Column(length = 10000)
    String content;
    String title;
    @Transient
    String shortContent;
    ... 
}

我希望得到特定作者的文章。

@edit

        id  content                                                  date           flagEditor  flagWriter  title   author_fk   editor_fk
        30  nweINSERT INTO `WRITERS` (`USER_ID`) VALUES<br>(9)...   2013-06-02 13:14:55     0   0   new     9   NULL
        31  INSERT INTO `WRITERS` (`USER_ID`) VALUES<br>(9);IN...   2013-06-02 13:20:04     0   0   dsfsafadsf  9   NULL
        32  sdf((User)session.getAttribute("user"))((User)sess...   2013-06-02 13:35:33     0   0   frefds  9   NULL

Hibernate SQL:http://pastebin.com/yfPz6aDb

好的我发现了问题:

public List<Article> findByCriteria(DetachedCriteria criteria){
    List<Article> articles = null;
    articles = criteria.getExecutableCriteria(HibernateUtil.getSession()).list();
//return value wasn't assignet do articles
    return articles;


}

感谢所有人的承诺:)

1 个答案:

答案 0 :(得分:1)

您传递的是作者标准,而不是传递根条件。代码应该是:

List<Article> articles = articleManager.findArticleByCriteria(criteria);