有两张表
文章表Content 
private Integer contentId;
private User user;(对应User表中username)
private String content;
private String contentTime;
private String contentTitle;
private Set groupsForContentTitle = new HashSet(0);
private Set groupsForAuthor = new HashSet(0);
private Set groupsForContentId = new HashSet(0);用户表User表
private Integer id;
private String username;
private String password;
private String sex;
private Integer age;
private String email;
private String question;
private String answer;
private Set contents = new HashSet(0);
现在有个问题:怎么根据用户名(username)查找用户的文章列表(content)
知道的说下 ,急 要求能通过的代码?
现在Content表中的是对象啊 怎么根据此对象查找文章表中的content(文章)
急!急!急!

解决方案 »

  1.   

    HQL: from Content where user.username='username'
      

  2.   


    大哥 明明表中有数据的 还是查出Null
      

  3.   


    明明有值还是null 郁闷弄了快一天了 一直Null
      

  4.   

    现在我改了外键 增加一列对应user中的id(还是被封装成对象了)Content表:
    private Integer contentId;
    private User user;(外键对应user表中的id)
    private String author;
    private String content;
    private String contentTime;
    private String contentTitle;
    private Set groups = new HashSet(0);User表:
    private Integer id;
    private String username;
    private String password;
    private String sex;
    private Integer age;
    private String email;
    private String question;
    private String answer;
    private Set contents = new HashSet(0);测试语句:
    先查出user再根据user查文章
    Session session = HibernateSessionFactory.getSession();
    Query query = session.createQuery("from User where username='admin'");
    List list2 = query.list();
    User u = null;
    for (int i = 0; i < list2.size(); i++) {
    u = (User) list2.get(0);
    }
    下面怎么写呢?怎么获取文章?
    Set t=u.getContents();?吗?
    代码...