一个用户表 (类名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 语句是如何? 我写来写去都不行啊
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 语句是如何? 我写来写去都不行啊
解决方案 »
- 如何手动用Eclipse 搭建SSH2框架 急急急
- traversing associations是什么东西?
- Io 异常: The Network Adapter could not establish the connection
- ssh2事务操作
- 会话丢失问题?
- 各位大哥,帮帮忙.....
- struts2.x和webwork有什么关系?
- javascript取不到文本框的值?
- 高分求救:在weblogic中定义实体bean之间的关系时的问题(up有分)
- 初次涉及struts,一些疑问,急救(在线立等)!
- 新手在线求教SSH框架(struts2.1 spring3.0 hibernate3.3)搭建问题
- Caused by: java.lang.NullPointerException无法解决在别的项目上能运行,这个是服务启动时候我想写入application
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)
private String create_day;
private String create_month;
//getter and setter
FROM Schedule as s join fetch s.User as u where u.id= 1 and (content like '%keyword%' or title like '%keyword%');
终于行啦
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+"%')";