我有4个表参与此查询。
Campaign
- 多对一个企业
Business
- 一对多客户
Client
- 一对一联系
Contact
在联系中,字段contact_name
是唯一的。我需要检索与campaigns
相关的所有contact
client
和business
campaign
字段type
等于2。
使用hibernate执行此操作的最佳方法是什么?
在SQL中看起来像这样:
select *
from campaign,contact, business, client
where campaign.type=2
and client.contact_id = contact.contact_id
and contact.name = 'Josh'
and client.business_id = business.business_id
and campaign.campaign_id = business.business_id
答案 0 :(得分:2)
您也可以使用Session的 createSQLQuery()方法执行本机SQL查询。
您还可以使用标量属性来避免使用ResultSetMetadata的开销 您可以从here
找到有关此内容的更多信息答案 1 :(得分:1)
我认为以下内容应该有效。
from Compaign where Compaign.type=2 and compaign.business.client.contact.contact_name=:name