JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Object Relational Mapping » Hibernate  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 hql中如何拼接字符串?
fsolsh





發文: 12
積分: 0
於 2010-05-28 18:04 user profilesend a private message to usersend email to fsolshreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
String hql = "from EmployeeModel as employeemodel where 0 = 0 "
+ " and employeemodel.joinPartyDate like concat('%', to_char(sysdate,'MM'))";

List list = this.getBHibernateTemplate().find(hql);

===============================

EmployeeModel中joinPartyDate在DB中对应一个varchar型的列(入党时间),数据形式:yyyy-MM;现在需要查询入党月份为当前月的所有员工。

where条件中我想到两种方法
1:对employeemodel.joinPartyDate取子串(只要后两个字符)用=与to_char(sysdate,'MM'))对比;
2:employeemodel.joinPartyDate LIKE concat('%', to_char(sysdate,'MM'));

经尝试,两种方法都是通不过的。我刚接触hibernate,对hql语法不太熟悉,不知道怎样写才能达到需求要求?请指教!


reply to postreply to post
BaoSight-KwanSee
作者 Re:hql中如何拼接字符串? [Re:fsolsh]
phuang3





發文: 38
積分: 0
於 2010-05-29 16:28 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問為何不用SimpleDateFormat將日期格式化成yyyy-MM的String然後直接用where employeemodel.joinPartyDate='2010-05'

reply to postreply to post
作者 Re:hql中如何拼接字符串? [Re:phuang3]
fsolsh





發文: 12
積分: 0
於 2010-05-30 19:27 user profilesend a private message to usersend email to fsolshreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
谢谢 phuang3
需求是这样的,每个月的1号给当月入党的党员发送廉政格言,至于哪一年入党是不管的,也就是说2010-06-01 早上8:00要给所有在6月份(不管是2000年入党,还是2009年入党)入党的员工发送廉政格言。
所以我才想要截取比较,或者拼接like

请大家指教


reply to postreply to post
BaoSight-KwanSee
作者 Re:hql中如何拼接字符串? [Re:fsolsh]
phuang3





發文: 38
積分: 0
於 2010-06-02 17:56 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
其實HQL跟SQL與法很像,用 where xxx like '%-06' 是確定可以的,只是不知為何您會有問題? 先用SQL試試,可以的話直接套上HQL。

reply to postreply to post
作者 Re:hql中如何拼接字符串? [Re:phuang3]
fsolsh





發文: 12
積分: 0
於 2010-06-03 08:33 user profilesend a private message to usersend email to fsolshreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
where xxx like '%-06' 确实是可以的,但是%和月份拼起来这个操作在这同一句hql中写不出来。
hql中类似CONCAT('%', to_char(sysdate, 'MM'))这个东西是用不了的,直接用||来拼还是不行的。不知道明白我意思不?


reply to postreply to post
BaoSight-KwanSee
作者 Re:hql中如何拼接字符串? [Re:fsolsh]
phuang3





發文: 38
積分: 0
於 2010-06-05 12:41 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
不好意思,我沒用過HQL內建的function,這點可能要請站內的高手來解。
不過我習慣簡化HQL,也就是like後的字串在組HQL之前就決定了。


reply to postreply to post
» JWorld@TW »  Object Relational Mapping » Hibernate

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8