一个用户表 (类名user), 一个 schedule表 一对多关系 (类名schedule)
wvt_user:
userid , username ....   wvt_schedule:
id , userid , title , content 
SQL:SELECT * FROM( SELECT * FROM wvt_schedule  WHERE userid =1 ) s WHERE  content LIKE '%keyword%' ||  title LIKE '%keyword%';模糊查询:
条件是: 查询出wvt_schedule 指定的userid 中,content 和title 有 “keyword” 的请问hibernate 的HQL 语句是如何?  我写来写去都不行啊

解决方案 »

  1.   

    FROM schedule as s,user as u where s.userid = u.userid and s.userid= 1 and (content like '%keyword%' or title like '%keyword%');
      

  2.   

    生成SQL 语句是成功的, 但是 在返回集合的时候就出现了一点问题 , 麻烦看看public List<Schedule> findByKeyWord(User user ,String keyWord, final int firstResult,
    final int maxResults) {
    System.out.println(user.getUserid()+ "=======");
    String hql = "FROM Schedule as s, User as u where s.user.userid = u.userid and s.user.userid = 1 and (content like '%"+keyWord+"%' or title like '%"+keyWord+"%')";
    return (List<Schedule>) getHibernateTemplate().find(hql);
    }
    Test:User user = new User();
     user.setUserid(1);
    List<Schedule> list = sd.findByKeyWord (user,"Cont",0,9);
     for (int i = 0; i < list.size(); i++) {

     System.out.println(list.get(i).getTitle()  + " " + list.get(i).getUser().getUserid());
     }
    控制台:
    1=======
    Hibernate: 
        select
            schedule0_.id as id2_0_,
            user1_.userid as userid0_1_,
            schedule0_.title as title2_0_,
            schedule0_.content as content2_0_,
            schedule0_.re as re2_0_,
            schedule0_.complete as complete2_0_,
            schedule0_.target_time as target6_2_0_,
            schedule0_.complete_time as complete7_2_0_,
            schedule0_.create_time as create8_2_0_,
            schedule0_.create_day as create9_2_0_,
            schedule0_.create_month as create10_2_0_,
            schedule0_.userid as userid2_0_,
            user1_.username as username0_1_,
            user1_.password as password0_1_,
            user1_.credits as credits0_1_,
            user1_.lastvisit as lastvisit0_1_,
            user1_.lastIP as lastIP0_1_ 
        from
            wvt_schedule schedule0_,
            wvt_user user1_ 
        where
            schedule0_.userid=user1_.userid 
            and schedule0_.userid=1 
            and (
                content like '%Cont%' 
                or title like '%Cont%'
            )
    Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to org.webvigator.entity.Schedule
    at org.webvigator.test.ScheduleTest.main(ScheduleTest.java:54)
      

  3.   

    Schedule   类如下:public class Schedule  implements Serializable{ private static final long serialVersionUID = 1L; private int id; private User user; private String title; private String content; private String re; private boolean complete; private Date target_time; private Date complete_time; private Date create_time;

    private String create_day;

    private String create_month;
    //getter and setter
      

  4.   

    如果 用那什么 join fetch 会是如何整?
      

  5.   

    用join fetch做得,你试试
    FROM Schedule as s join fetch s.User as u where  u.id= 1 and (content like '%keyword%' or title like '%keyword%');
      

  6.   

    谢谢 6楼的同学 哈哈哈
    终于行啦
    final String hql = "FROM Schedule as s join fetch s.user as u where u.userid= "+user.getUserid()+" and (content like '%"+keyWord+"%' or title like '%"+keyWord+"%')";