HQL左连接:预计加入的路径 [英] HQL left join: Path expected for join

查看:71
本文介绍了HQL左连接:预计加入的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我尝试左连接2个表格,患者和提供者,并且继续获取HQL左连接加入的路径!第二张桌上的错误。欣赏它,如果有人能帮助解决这个问题!



以下是两个表/类的映射:

patient.hbm.xmL:

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping package =com.ccg.db.test>
< class name =patienttable =patient>
< id name =patientIdcolumn =patientIdtype =int>
< generator class =native/>
< / id>
< property name =patientNametype =string>
< column name =patientName/>
< / property>
< property name =providerIdtype =string>
< column name =provId/>
< / property>
<多对一名称=providercolumn =providerIdclass =provider/>
< / class>
< / hibernate-mapping>






provider.hbm.xml:

 <?xml version =1.0?> 
<!DOCTYPE hibernate-mapping PUBLIC - // Hibernate / Hibernate Mapping DTD 3.0 // ENhttp://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd\">
< hibernate-mapping package =com.ccg.db.test>
< class name =providertable =provider>
< id name =providerIdcolumn =providerId>
< generator class =native/>
< / id>
< property name =providerNamecolumn =providerName/>
< / class>
< / hibernate-mapping>






pojo:

  patient.java 

package com.ccg.db.test;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;

公共类患者
实现Serializable
{
private int patientId;
私人字符串patientName;
私人字符串providerId; //提供商的外键

private static final long serialVersionUID = 81073;

public static void load(Session session,List< String> values){
patient PatientInfo = new patient();

PatientInfo.setPatientId(Integer.parseInt(values.get(0)));
PatientInfo.setPatientName(values.get(1));
PatientInfo.setProviderId(values.get(2));

session.save(PatientInfo);

$ b / **
* @return PatientId
* /
public int getPatientId(){
return patientId;

$ b $ **
* @param PatientId PatientId设置
* /
public void setPatientId(int PatientId){
this.patientId = PatientId;

$ b / **
* @返回PatientName
* /
public String getPatientName(){
return this.patientName;

$ b $ **
* @param PatientName PatientName设置
* /
public void setPatientName(String PatientName){
this.patientName = PatientName;

$ b / **
* @返回ProvId
* /
public String getProviderId(){
return this.providerId;

$ b $ **
* @param id ProviderId设置
* /
public void setProviderId(String id){
this.providerId = id;

$ b / **
* @返回ProvId
* /
public String getProvider(){
return this.providerId;

$ b $ **
* @param id ProviderId设置
* /
public void setProvider(String id){
this.providerId = id;

$ b $ **
* @param args
* /
public static void main(String [] args){
/ / TODO自动生成的方法存根
}
}

provider.java:

package com.ccg.db.test;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

公共类提供者
实现Serializable
{
private String providerId;
私人字符串providerName;

// private int patientId;
// private int providerSpec;

private static final long serialVersionUID = 81073;

public static void load(Session session,List< String> values){
provider ProviderInfo = new provider();

ProviderInfo.setProviderId(values.get(0));
ProviderInfo.setProviderName(values.get(1));
//ProviderInfo.setProviderSpec(Integer.parseInt(values.get(2)));

session.save(ProviderInfo);
}
$ b / **
* @返回ProviderName
* /
public String getProviderName(){
return providerName;

$ b $ **
* @param ProviderName提供者名称设置
* /
public void setProviderName(String name){
this.providerName = name;

$ b / **
* @返回ProvId
* /
public String getProviderId(){
return this.providerId;
}

/ **
* @param id ProvId设置
* /
public void setProviderId(String id){
this.providerId = id;
}

/ *
public int getPatientId(){
return this.patientId;
}

public void setPatientId(int id){
this.patientId = id;
}
* /

/ **
* @param args
* /
public static void main(String [] args) {
// TODO自动生成的方法存根
}
}



< hr>

以下是左连接查询:

  select 
pat .patientId,pat.patientName
from
患者为pat
左连接
提供者为pro
其中
pat.providerId = pro.providerId






这里是结果:

 
0:50:08,479 INFO查询:156 - Query = outerJoin
10:50:08,479信息查询:157 - 选择
pat.patientId ,pat.patientName
from
患者为pat
离开加入
提供者为pro
其中
pat.providerId = pro.providerId

10:50:08,698错误参数:33 - 加入的路径!
10:50:08,698错误参数:33 - 无效路径:'pro.providerId'
10:50:08,698错误参数:33 - 二元运算符的右侧操作数为空
10:50:08,698错误查询:184 - 生成查询的问题。
org.hibernate.hql.ast.QuerySyntaxException:加入的路径! [选择
pat.patientId,pat.patientName

com.ccg.db.test.patient作为pat
离开连接
提供者作为pro
其中
pat.providerId = pro.providerId
]
位于org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
位于org.hibernate.hql .ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl .analyze(QueryTranslatorImpl.java:235)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl .java:111)
at org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan。(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQue ryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.ccg.db.query.QueryManager.query(QueryManager.java:163)
at com.ccg.db.query.QueryManager.query(QueryManager.java:139)
at com.ccg.db.query.QueryManager.main(QueryManager.java:80)


解决方案

您的患者有提供者的参考,并且还有提供者ID作为财产。我可能会摆脱病人的提供者id属性,只是提供给提供者。然后你的查询应该是这样的。


$ b

  select pat.patientId,pat.patientName $ b从病人身上获得$ b作为拍卖b 
加入pat.provider作为pro

需要从您的患者到您的提供者(在这种情况下是pat.provider)的关联路径。然后,hibernate将自动使用多对一映射中指定的列加入提供程序表。在你的情况下,由于你没有查询提供者的任何属性,所以连接没有多大意义,所以像这样的东西可能会更有意义

  select pat 
from pat as
加入pat.provider as pro
pat.patientName ='John'
和pro.name ='United Healthcare'

您可以将患者列表过滤为名为John的患者将United Healthcare作为提供者。

I am new at Hibernate, and I have a question regarding HQL Left join.

I try to left join 2 tables, patient and provider, and keep getting "Path expected for join!" error on the second table. Appreciate it if anybody can help on this issue!

Here is the mapping of the 2 tables/classes:

patient.hbm.xmL:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ccg.db.test">
    <class name="patient" table="patient">
        <id name="patientId" column="patientId" type="int">
            <generator class="native"/>
        </id>
        <property name="patientName" type="string" >
           <column name="patientName" /> 
        </property>
        <property name="providerId" type="string" >
            <column name="provId" /> 
        </property>
        <many-to-one name="provider" column="providerId" class="provider" /> 
    </class>
</hibernate-mapping>


provider.hbm.xml:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ccg.db.test">
    <class name="provider" table="provider">
        <id name="providerId" column="providerId">
            <generator class="native" />
        </id>
        <property name="providerName" column="providerName" />
    </class>
</hibernate-mapping>


pojo:

patient.java

package com.ccg.db.test;

import java.io.Serializable;
import java.util.List;
import org.hibernate.Session;

public class patient 
implements Serializable
{
    private int patientId;
    private String patientName;
    private String providerId; // foreign key to provider

    private static final long serialVersionUID = 81073;

    public static void load(Session session, List<String> values){
        patient PatientInfo = new patient(); 

        PatientInfo.setPatientId(Integer.parseInt(values.get(0)));
        PatientInfo.setPatientName( values.get(1));
        PatientInfo.setProviderId( values.get(2) );

        session.save( PatientInfo );
    }

    /**
    * @return the PatientId
    */
    public int getPatientId() {
        return patientId;
    }

    /**
    * @param PatientId the PatientId to set
    */
    public void setPatientId(int PatientId) {
        this.patientId = PatientId;
    }

    /**
    * @return the PatientName
    */
    public String getPatientName() {
        return this.patientName;
    }

    /**
    * @param PatientName the PatientName to set
    */
    public void setPatientName(String PatientName) {
        this.patientName = PatientName;
    }

    /**
    * @return the ProvId
    */
    public String getProviderId() {
        return this.providerId;
    }

    /**
    * @param id the ProviderId to set
    */
    public void setProviderId( String id ) {
        this.providerId = id;
    }

    /**
    * @return the ProvId
    */
    public String getProvider() {
        return this.providerId;
    }

    /**
    * @param id the ProviderId to set
    */
    public void setProvider( String id ) {
        this.providerId = id;
    }  

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

provider.java:

package com.ccg.db.test;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Session;

public class provider 
implements Serializable
{
    private String providerId;
    private String providerName;

    //private int patientId;
    //private int providerSpec; 

    private static final long serialVersionUID = 81073;

    public static void load(Session session, List<String> values){
        provider ProviderInfo = new provider(); 

        ProviderInfo.setProviderId( values.get(0) );
        ProviderInfo.setProviderName( values.get(1));
        //ProviderInfo.setProviderSpec( Integer.parseInt(values.get(2)) );

        session.save( ProviderInfo );
    }

    /**
    * @return the ProviderName
    */
    public String getProviderName() {
        return providerName;
    }

    /**
    * @param ProviderName the ProviderName to set
    */
    public void setProviderName(String name) {
        this.providerName = name;
    }

    /**
    * @return the ProvId
    */
    public String getProviderId() {
        return this.providerId;
    }

    /**
    * @param id the ProvId to set
    */
    public void setProviderId( String id ) {
        this.providerId = id;
    }

    /*
    public int getPatientId() {
        return this.patientId;
    }

    public void setPatientId( int id ) {
        this.patientId = id;
    }
    */

    /**
    * @param args
    */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}


Here is the left join query:

select
    pat.patientId, pat.patientName
from
    patient as pat 
left join
    provider as pro 
where
    pat.providerId = pro.providerId


and here is the result:

0:50:08,479 INFO query:156 - Query = outerJoin
10:50:08,479 INFO query:157 - select
pat.patientId, pat.patientName
from
patient as pat 
left join
provider as pro 
where
pat.providerId = pro.providerId

10:50:08,698 ERROR PARSER:33 - Path expected for join!
10:50:08,698 ERROR PARSER:33 - Invalid path: 'pro.providerId'
10:50:08,698 ERROR PARSER:33 - right-hand operand of a binary operator was null
10:50:08,698 ERROR query:184 - Problem generating query.
org.hibernate.hql.ast.QuerySyntaxException: Path expected for join! [select
pat.patientId, pat.patientName
from
com.ccg.db.test.patient as pat 
left join
provider as pro 
where
pat.providerId = pro.providerId
]
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:31)
at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:24)
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:235)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:160)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:111)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)
at com.ccg.db.query.QueryManager.query(QueryManager.java:163)
at com.ccg.db.query.QueryManager.query(QueryManager.java:139)
at com.ccg.db.query.QueryManager.main(QueryManager.java:80)

解决方案

Your patient has a reference to the provider, and also has the provider id as a property. I would probably get rid of the provider id property on the patient and just have the reference to the provider. Then your query should be something like this.

select pat.patientId, pat.patientName 
from patient as pat 
left join pat.provider as pro

To join, you need the association path from your patient to your provider, which in this case is pat.provider. Then hibernate will automatically use the "column" specified in the many-to-one mapping to join into the provider table. In your case, the join doesn't make a lot of sense since you aren't querying on any properties of the provider, so something like this might make more sense

select pat 
from patient as pat 
join pat.provider as pro 
where pat.patientName = 'John' 
and pro.name = 'United Healthcare'

There you are able to filter your patient list to patients named John that have United Healthcare as the provider.

这篇关于HQL左连接:预计加入的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆