三个连接表在JPA中查询多对多关系

时间:2016-04-03 12:26:59

标签: java mysql sql jpa

有三个表:HospitalMedical_ServiceLanguage_Service, 医院可以提供医疗服务和语言服务。所以有两对多关系。

ERD Image

现在我想通过三个条件搜索医院:邮政编码,医疗和语言,我该如何编写这个SQL。

2 个答案:

答案 0 :(得分:0)

应该是这样的:

<div ng-app="myApp" ng-controller="Ctrl">
  {{ctrlTest}}
  <span ng-show="!allTrue">X</span>
  <span ng-show="allTrue"></span>
  <p>
  {{filter.brewers}}
  </p>
  <hr/>
  <div>
    <div>
      <li ng-repeat="(key,value) in filter.producers" ng-show="value">{{key}}
        <a ng-click="hideMe(key)"> X</a> // call to set this key false
      </li>
    </div>
    {{filter.producers}}
  </div>
</div>

假设您的实体映射正确。 此外,SELECT h FROM Hospital h JOIN h.medicalService m JOIN h.languageService l WHERE h.postcode = :postCodeParameter AND m.medicalName = :medicalNameParameter AND l.languageName = :languageNameParameter HM_id列是不必要的,它应该加入主键。

答案 1 :(得分:0)

我认为你的SQL看起来像这样:

SELECT * FROM Hospital WHERE Postcode = 3000 AND
Hospital_id IN
    (SELECT Hospital_id FROM Hospital_Medical hm
    INNER JOIN Medical_Service m ON hm.Medical_id = m.Medical_id
    where Medical_name = 'Emergency') AND
Hospital_id IN
(SELECT Hospital_id FROM Hospital_Language hl
    INNER JOIN Language_Service l ON hl.Language_id = l.Language_id
    where Language_name = 'English')