【问题标题】:inner join is not working in hql in hibernate内部联接在休眠中的 hql 中不起作用
【发布时间】:2015-02-18 04:31:47
【问题描述】:

我想在休眠中使用 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;
    }
}

组织类型 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>

组织类型表的映射文件

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;
    }

}

组织类型映射文件:

<?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)

【问题讨论】:

  • 为什么你没有在 POJO org 中为 set 创建 setter 和 getter?
  • 它出现在我的映射文件中,但它的书写错误。对不起

标签: java hibernate hql inner-join


【解决方案1】:

试试这个

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

【讨论】:

  • 不,它也无法正常工作......它给出错误:错误:连接的预期路径!错误:预期加入的路径!预期加入的路径!
  • 路径在查询中被修改。试一试。
  • 现在它给出了这个错误:警告:/index.xhtml @63,120 actionListener="#{managedBean.insertf}": java.lang.ClassCastException: [Ljava.lang.Object;不能转换为 pojo.Orgtype
  • 现在查询似乎是正确的,新错误是一个强制转换异常,因为您需要添加更多代码来存储查询结果。
【解决方案2】:

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" );

【讨论】:

  • 如果不行,试试INNER JOIN fetch 其余的都一样
【解决方案3】:

试试这个

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

【讨论】:

  • 请在您的回答中添加一些细节。
猜你喜欢
  • 1970-01-01
  • 2012-09-06
  • 1970-01-01
  • 2020-06-20
  • 2015-03-13
  • 2019-10-28
  • 1970-01-01
  • 2012-06-14
  • 1970-01-01
相关资源
最近更新 更多