求帮忙看看这个HQL语句该怎么写

charlesluo2012 2012-05-13 12:49:36
两张表:一张user表,y一张account表,他们之间是一对多的双向关联,user表有user_id字段为主键,account表有account_id为主键,user_id为外键, 两个表是由对应持久类自动生成的, 我现在要从account表中根据指定的user_id 查询出此user_id的account记录。。
我是这样写的("from Account as a where a.user_id=?",id) 不行,

另外这样写也不行:this.getHibernateTemplate().find("from Account as a where a.user_id in (select u.account.user_id from User as u where u.user_id=?)",id);

一样是提示:could not resolve property:user_id;


注意,持久类中也是user_id,但是运行的时候,出现异常,说could not resolve property: user_id ,

我检查了数据库中的表的字段,account表明明有user_id字段为外键,为什么会提示找不到这个字段??请熟悉的人帮忙看看。。毕设大限将至,感激不尽!
...全文
142 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenhu87 2012-05-16
  • 打赏
  • 举报
回复
("from Account a where a.user.id=?",id)
monrove 2012-05-16
  • 打赏
  • 举报
回复
应该这么写:
("from Account a where a.user.user_id=?",id)
楼主确定你的Account类中,属性是user_id,而不是user?
那怎么映射的一对多?
dreamhyz 2012-05-15
  • 打赏
  • 举报
回复
呵呵,看映射文件吧~~~可能写了数据库表中的啦,有可能实体不是这个咯
hugo000002020 2012-05-15
  • 打赏
  • 举报
回复
把你的Account持久类和映射文件贴出来看看
  • 打赏
  • 举报
回复
Account 这个对象里面根本就没有user_id这个属性 只有user 然后根据user找id
  • 打赏
  • 举报
回复
("from Account a where a.user.id=?",id)
其中user是你Account 这个类里面的一个User对象的属性
NoTargetException 2012-05-13
  • 打赏
  • 举报
回复
应该就是mapping问题。
liu472 2012-05-13
  • 打赏
  • 举报
回复
一样是提示:could not resolve property:user_id;
这里的user_id应该是你的model对应的对象,而不是数据库中字段
古布 2012-05-13
  • 打赏
  • 举报
回复

注意,持久类中也是user_id,但是运行的时候,出现异常,说could not resolve property: user_id ,
持久类改为 userId,
HQL ("from Account as a where a.userId=?",id)
试试
结贴是美德 2012-05-13
  • 打赏
  • 举报
回复
按外键user_id查出Account,然后直接导航,Account.getUsers().getAccounts()
dqsweet 2012-05-13
  • 打赏
  • 举报
回复
是不是mapping的问题?
charlesluo2012 2012-05-13
  • 打赏
  • 举报
回复
上面的老兄,我上面说了,照您这个写法不行的。。。是不是根据主表外键不能找到主表记录啊。。。。
dqsweet 2012-05-13
  • 打赏
  • 举报
回复
("from Account a where a.user_id=?",id)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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