0

我正在使用 JPA 和 Hibernate,但我的需求太复杂而无法构建 HQL,至少对我来说是这样。我决定使用原生 SQL。我需要用他们的子列表获取一些记录/实体,但是,我需要过滤孩子,我不想要所有的孩子。这可能吗?

例如我有三个表

对话表:id

邮件表:id、conversation_id、sender_id

user_table:id,married_bool

我需要具有由已婚用户发送的conversation由 s 填充的列表的实体。mail

我试过到目前为止

Select * from conversation_table join mail_table on ( mail_table.sender_id=user_table.id and user_table.married_bool=true) 

我收到如下错误:

user_table is unknown

我试过了

Select * from (conversation_table, user_table, mail_table) join mail_table on ( mail_table.sender_id=user_table.id and user_table.married_bool=true)

我正进入(状态

mail_table is non unique table/alias

这只是一个简单的例子,还需要在其他表中填充邮件实体的一些实体列表,例如接收者表,我想我必须在本机查询中添加连接子句来填充这些,对吗?当尝试这个额外的连接子句时,我也会收到“未知”/“非唯一别名”错误。

感谢您的建议。

4

2 回答 2

1

像这样的东西:

SELECT *
FROM conversation_table CT
   JOIN mail_table MT
      ON CT.Id = MT.conversation_id
   JOIN user_id UI
      ON MT.sender_id=UI.id
WHERE UT.married_bool = 1 
于 2013-01-04T12:37:22.187 回答
1

如果你真的想对这个查询使用 Hibernate ORM,那么 Criteria Query 是最好的方法。最近,我有同样的要求,只使用了 Criteria。

于 2013-01-04T13:15:04.083 回答