-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: How to do two joins in same query?
PostPosted: Thu Jan 08, 2004 8:33 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
I'm trying to join MpLote with Mp and ArmazemMp in the same query.
As the mapping file shows, MpLote has a "many-to-one" relation with Mp and a "one-to-one" relation with ArmazemMp.

I've tried diferent ways but it keeps giving me:
Code:
net.sf.hibernate.QueryException
( ... )


Can someone please help me on this?

------------------
Testing Code
------------------

Code:
( ... )
SessionFactory sessionFactory = HibernateFactory.createFactory();
session = sessionFactory.openSession();
         
res = session.find("select ml " +
         "from vo.MpLote as ml join ml.mp as mp, " +
         "join ml.armazemMp " +
         "where mp.referencia = '" + ref + "' order by ml.referencia asc");
( ... )


----------------------------------------
Mapping files (generated with R3)
----------------------------------------

MpLote
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.MpLote"
    table="mp_lote"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="referencia"
        type="java.lang.String"
        column="referencia"
        not-null="true"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional many-to-one association to Mp -->
    <many-to-one
        name="mp"
        class="vo.Mp"
        not-null="true"
    >
        <column name="mp_fk" />
    </many-to-one>
   
    <!-- bi-directional one-to-one association to ArmazemMp -->
    <one-to-one
        name="armazemMp"
        class="vo.ArmazemMp"
        outer-join="auto"
        constrained="false"
    />

</class>
</hibernate-mapping>


Mp
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.Mp"
    table="mp"
>

    <id
        name="id"
        type="long"
        column="id"
    >
        <generator class="increment" />
    </id>

    <property
        name="referencia"
        type="java.lang.String"
        column="referencia"
        not-null="true"
        length="-1"
    />

    <!-- associations -->
    <!-- bi-directional one-to-many association to MpLote -->
    <set
        name="mpLotes"
        lazy="true"
        inverse="true"
    >
        <key>
            <column name="mp_fk" />
        </key>
        <one-to-many
            class="vo.MpLote"
        />
    </set>

</class>
</hibernate-mapping>


ArmazemMp
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
   
<hibernate-mapping>
<!--
    Created by Middlegen Hibernate plugin

    http://boss.bekk.no/boss/middlegen/
    http://hibernate.sourceforge.net/
-->

<class
    name="vo.ArmazemMp"
    table="armazem_mp"
>

    <id
        name="mpLoteFk"
        type="long"
        column="mp_lote_fk"
    >
        <generator class="assigned" />
    </id>

    <property
        name="localizacao"
        type="java.lang.String"
        column="localizacao"
        length="10"
    />

    <!-- associations -->
    <!-- bi-directional one-to-one association to MpLote -->
    <one-to-one
        name="mpLote"
        class="vo.MpLote"
        outer-join="auto"
    />

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 8:53 am 
Expert
Expert

Joined: Fri Nov 07, 2003 4:24 am
Posts: 315
Location: Cape Town, South Africa
Could you provide the full stack trace in DEBUG mode please.

Thanks

Justin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 10:22 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
drj wrote:
Could you provide the full stack trace in DEBUG mode please.

Thanks

Justin


Where's the stack trace:
Code:
net.sf.hibernate.QueryException: unexpected token: join [select ml from vo.MpLote as ml join ml.mp as mp, join ml.armazemMp where mp.referencia like upper('%ref%') order by ml.referencia asc]
   at net.sf.hibernate.hql.FromParser.token(FromParser.java:53)
   at net.sf.hibernate.hql.ClauseParser.token(ClauseParser.java:87)
   at net.sf.hibernate.hql.PreprocessingParser.token(PreprocessingParser.java:123)
   at net.sf.hibernate.hql.ParserHelper.parse(ParserHelper.java:29)
   at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:149)
   at net.sf.hibernate.hql.QueryTranslator.compile(QueryTranslator.java:138)
   at net.sf.hibernate.impl.SessionFactoryImpl.getQuery(SessionFactoryImpl.java:293)
   at net.sf.hibernate.impl.SessionImpl.getQueries(SessionImpl.java:1493)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1464)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1454)
   at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1446)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 10:27 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Remove the comma between "mp, join".

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2004 10:57 am 
Pro
Pro

Joined: Wed Oct 08, 2003 10:31 am
Posts: 247
christian wrote:
Remove the comma between "mp, join".


THANKS very much Christian!
It worked.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.