内联接在hibernate中的hql中不起作用

时间:2015-02-18 04:31:48

标签: java hibernate hql inner-join

我想在hibernate中使用HQL中的内连接从两个表中获取数据但是它不工作不知道为什么? Pojo类及其映射文件给出:

public class Org  implements java.io.Serializable {


     private String quarter;
     private Orgtype orgtype;
     private String parent;
     private Set regions = new HashSet(0);
     private Set cfgOrgObjects = new HashSet(0);

    public Org() {
    }


    public Org(String quarter, Orgtype orgtype, String parent) {
        this.quarter = quarter;
        this.orgtype = orgtype;
        this.parent = parent;
    }
    public Org(String quarter, Orgtype orgtype, String parent, Set regions, Set cfgOrgObjects) {
       this.quarter = quarter;
       this.orgtype = orgtype;
       this.parent = parent;
       this.regions = regions;
       this.cfgOrgObjects = cfgOrgObjects;
    }

    public String getQuarter() {
        return this.quarter;
    }

    public void setQuarter(String quarter) {
        this.quarter = quarter;
    }
    public Orgtype getOrgtype() {
        return this.orgtype;
    }

    public void setOrgtype(Orgtype orgtype) {
        this.orgtype = orgtype;
    }
    public String getParent() {
        return this.parent;
    }

    public void setParent(String parent) {
        this.parent = parent;
    }
}

Orgtype pojo类:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 4, 2015 2:30:35 AM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="pojo.Org" table="ORG" schema="myschema">
        <id name="quarter" type="string">
            <column name="QUARTER" length="600" />
            <generator class="assigned" />
        </id>
        <many-to-one name="orgtype" class="pojo.Orgtype" fetch="select">
            <column name="TYPE" length="30" not-null="true" />
        </many-to-one>
        <property name="parent" type="string">
            <column name="PARENT" length="30" not-null="true" />
        </property>
 </class>
</hibernate-mapping>

Orgtype表的映射文件

public class Orgtype implements java.io.Serializable {

    private String type;
    private String description;
//    private Set orgs = new HashSet(0);
    private Set<Org> orgs = new HashSet<Org>();

    public Orgtype() {
    }

    public Orgtype(String type, String description) {
        this.type = type;
        this.description = description;
    }

    public Orgtype(String type, String description, Set orgs) {
        this.type = type;
        this.description = description;
        this.orgs = orgs;
    }

    public String getType() {
        return this.type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getDescription() {
        return this.description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set getOrgs() {
        return this.orgs;
    }

    public void setOrgs(Set orgs) {
        this.orgs = orgs;
    }

}

Orgtype映射文件:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Feb 4, 2015 2:30:35 AM by Hibernate Tools 3.6.0 -->
<hibernate-mapping>
    <class name="pojo.Orgtype" table="ORGTYPE" schema="myschema">
        <id name="type" type="string">
            <column name="TYPE" length="30" />
            <generator class="assigned" />
        </id>
        <property name="description" type="string">
            <column name="DESCRIPTION" length="600" not-null="true" />
        </property>
        <set name="orgs" table="ORG" inverse="true" lazy="true" fetch="select">
            <key>
                <column name="TYPE" length="30" not-null="true" />
            </key>
            <one-to-many class="pojo.Org" />
        </set>
    </class>
</hibernate-mapping>

我正在使用的查询是:

 Query q = sess.createQuery("SELECT og.quarter,og.parent,ogt.type,ogt.description FROM Org og INNER JOIN Orgtype ogt ogt.type = og.Orgtype" );

错误:

ERROR:   line 1:94: unexpected token: ogt
ERROR:   line 1:94: unexpected token: ogt
line 1:94: unexpected token: ogt
    at org.hibernate.hql.internal.antlr.HqlBaseParser.fromJoin(HqlBaseParser.java:1694)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.fromClause(HqlBaseParser.java:1349)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.selectFrom(HqlBaseParser.java:1055)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.queryRule(HqlBaseParser.java:701)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.selectStatement(HqlBaseParser.java:294)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:157)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:268)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:182)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:138)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
    at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
    at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:221)
    at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:199)
    at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1778)
    at managedBean.ManagedBean.insertf(ManagedBean.java:140)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:275)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:147)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:818)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:790)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1282)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:357)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:260)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:188)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:191)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:168)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:189)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:288)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:206)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:136)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:114)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:838)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:113)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:564)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
    at java.lang.Thread.run(Thread.java:744)

3 个答案:

答案 0 :(得分:0)

试试这个

Query q = sess.createQuery("SELECT og.quarter,og.parent,ogt.type,ogt.description FROM Org og INNER JOIN og.orgtype ogt");

答案 1 :(得分:0)

Orgtype就像pojo.Org类的属性,你应该在查询中以某种方式提及它。 试试这个:

Query q = sess.createQuery("SELECT og.quarter,og.parent,ogt.type,ogt.description FROM Org og INNER JOIN og.orgtype as ogt where ogt.type = og.orgtype" );

答案 2 :(得分:0)

试试这个

  

代码   Query q = sess.createQuery("SELECT og.quarter,og.parent,ogt.type,ogt.description FROM Org og,Orgtype ogt where ogt.type = og.orgtype");