hibernate sql语句求助

legend_jhz 2008-11-24 06:31:19
现在有两张相关联的数据库表,jz_buisness_plan和jz_stream,映射文件如下:

public class JzBusinessPlan implements java.io.Serializable {
private Integer gpId;
private Date plandate;
private String remark;
private Set jzStreams = new HashSet(0);

public class JzStream implements java.io.Serializable {
private Integer opId;
private JzBusinessPlan jzBusinessPlan;
private String results;
private String comments;
private Date opTime;

现在需要从表Jz_business_plan中查找所有满足不在jzstream表中的jzbusinessplan的信息,我该怎么写?谢谢!
...全文
184 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
不是都睡觉了吧。
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
mark
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
帮我看看吧。
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lihan6415151528 的回复:]
用 not in查询是很方便的

具体例子 :
select a.* from a where a.id not in (select id from b where…..)

楼主这个映射看来是一对多关系 不难的
[/Quote]
我照你说的写成如下:

public List getUndoBusiness(int currentPage, int lineSize,int aid) {
List undoBusiness = new ArrayList();
AccountTable account = getAccountTable(aid);
String hql = "select a.* from jz_business_plan a where (a.gp_id not int (select gp_id from jz_stream))";
Query q = super.getSession().createQuery(hql);
q.setParameter(0,account);
q.setFirstResult((currentPage - 1) * lineSize);
q.setMaxResults(lineSize);
undoBusiness = q.list() ;
return undoBusiness;
}

结果报如下错误:

org.hibernate.hql.antlr.HqlBaseParser.recover(Lantlr/RecognitionException;Lantlr/collections/impl/BitSet;)V

我网上搜一下说是不匹配,无语了,因为要求用ssh,所以不知道怎么改两张表互联的问题。
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
dsd
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 lihan6415151528 的回复:]
用 not in查询是很方便的

具体例子 :
select a.* from a where a.id not in (select id from b where…..)

楼主这个映射看来是一对多关系 不难的
[/Quote]?
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
up
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
mark
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
郁闷
ppl2046 2008-11-25
  • 打赏
  • 举报
回复
不是很清楚
zjc_love 2008-11-25
  • 打赏
  • 举报
回复
hql查询返回的是对象
所以感觉直接用not in(id)应该不可取
或者你可以直接写sql语句
zjc_love 2008-11-25
  • 打赏
  • 举报
回复
hql语句:
先通过from JzStream js,JzBusinessPlan jbp where js.jzBusinessPlan = jbp;获取对象列表
然后保存列表中jzBusinessPlan的 id 到一数组
from JzBusinessPlan jbp where jbp.gpId not in(数组)
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jumpheightway 的回复:]
from JzBusinessPlan where jzStreams not in(from JzStrea)
应该是这样的
[/Quote]
这样写会报一下错误:
 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'not in  (select jzstream2_.op_id from qdrk.jz_stream jzstream2_))
legend_jhz 2008-11-25
  • 打赏
  • 举报
回复
legend_jhz 2008-11-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jumpheightway 的回复:]
from JzBusinessPlan where jzStreams not in(from JzStrea)
应该是这样的
[/Quote]
这样写不对吧?
legend_jhz 2008-11-24
  • 打赏
  • 举报
回复
select a.gpId from JzBusinessPlan where a.gpId not in (select jzBusinessPlan.gpId from JzStream)
好像不行。
jumpheightway 2008-11-24
  • 打赏
  • 举报
回复
from JzBusinessPlan where jzStreams not in(from JzStrea)
应该是这样的
legend_jhz 2008-11-24
  • 打赏
  • 举报
回复
就是没想出来才问的。
ma_liang 2008-11-24
  • 打赏
  • 举报
回复
使用hsql语句,这种hibernate使用的语句和标准的sql语句很相似,只是用对象来代替表名,可以参考一下hibernate相关书籍。
加载更多回复(1)

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧