我有这个在mysql中成功运行的查询,但我试图把它写成JPA查询,我一直收到错误。这是我的表格。
Table 1: business_accounts{id, business_name}
Table 2: work_locations{location_id, name, contractor_id }
//contractor_id on Table2 is the foreign key matched to id on table 1.
我的sql查询是我要返回business_name的所有值,其中表1中的id等于表2中的contractor_id,其中name =" Dublin"这是我在mysql中使用的SQL查询:
SELECT b.business_name FROM work_locations w
inner join business_accounts b on b.id=w.contractor_id where name="Carlow"
以下是我在JPA中采用的方法,该方法不起作用。任何建议表示赞赏。感谢
String countyName="Dublin";
Query myQuery2 = em.createQuery("SELECT b FROM business_accounts b join "
+ "w where b.id=w.contractor_id and w.name=:countyName");
myQuery2.setParameter("countyName", countyName);
答案 0 :(得分:0)
请尝试以下代码:
createNativeQuery
使用将成为返回类型的查询添加ENTITY CLASS。
String QUERY = "SELECT b.business_name FROM business_accounts b join w where b.id=w.contractor_id and w.name=?)";
List list = em.createNativeQuery(QUERY, ENTITY_CLASS_HERE.class)
.setParameter(1, countyName)
.getResultList();
答案 1 :(得分:0)
湾Work_Location是另一个
℃。您可以选择为a之间的连接创建单独的连接表。和b。我会建议,但这是你的偏好。
基于a。和b。尝试这些方面的东西 -
String countyName="Dublin";
final TypedQuery<String> query = entityManager
.createQuery(
"Select distinct b.business_name from Business_Account b, IN(b.id) location where b.id=location and location.name =:countyName", String.class); //$NON-NLS-1$
query.setParameter("countyName", countyName); //$NON-NLS-1$