【问题标题】:HQL many to many between 3 tables3个表之间的HQL多对多
【发布时间】:2015-07-19 08:07:59
【问题描述】:

我有 3 张桌子:

用户 ->>- 多对多 ->- 用户应用程序 ->>- 多对多 ->>- 应用程序

用户拥有:

  • 用户ID

  • 用户名

用户应用

  • 用户ID

  • applicationId

应用

  • applicationId

  • 应用程序名称

我没有成功创建返回一个特定用户的每个应用程序的 HQL 查询。

我的 HQL:

select a.userId, a.userName from Application b join b.userId a where b.userId = 1

简化我想做的查询:from Application WHERE Userapp.userID = 1

你能帮我吗:)?

编辑:

我的工具:

  • Netbean 8.x

  • 休眠插件

第二个错误:org.hibernate.hql.internal.ast.QuerySyntaxException: Userapp is not mapped

当我从数据库创建休眠映射文件和 POJO 时,它会为我创建 2 个对象:用户和应用程序。但不是关联表“Userapp”

我的 hibernate.reveng.xml :

<hibernate-reverse-engineering>
  <schema-selection match-catalog="allin"/>
  <table-filter match-name="user"/>
  <table-filter match-name="application"/>
  <table-filter match-name="userapp"/>
</hibernate-reverse-engineering>

问候

【问题讨论】:

  • 我觉得应该是User 1--* UserApp *--1 Application ;)
  • 我在之后更正它;)谢谢

标签: mysql sql hibernate many-to-many hql


【解决方案1】:

我认为您的查询应该是这样的:

SELECT a.applicaitonName
FROM User u
    LEFT JOIN UserApp ua ON u.userId= ua.userId
    LEFT JOIN Application a On ua.applicationId= a.applicationId
WHERE
    u.userName = ?

SELECT a.applicaitonName
FROM UserApp ua
    LEFT JOIN Application a On ua.applicationId= a.applicationId
WHERE
    ua.userId = ?

【讨论】:

  • 感谢您的回复。第二个问题是表“Userapp”...“Userapp 未映射”。我将 Netbean 与休眠一起使用,因此当我创建仅创建 2 个对象(用户和应用程序)的 POJO 文件时。未创建关联“Userapp”表...我必须手动执行?编辑:当我创建 hibernate.revenge.xml 我选择我的 3 个表(我在帖子上编辑它)
  • @ValentinGarreau for a new question 问一个新问题,它会更有意义;)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 2021-04-14
  • 1970-01-01
相关资源
最近更新 更多