-->
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.  [ 2 posts ] 
Author Message
 Post subject: Query a Many-to-Many Collection
PostPosted: Wed Mar 16, 2005 9:04 pm 
Regular
Regular

Joined: Tue Nov 30, 2004 4:23 pm
Posts: 62
I am messing around with the "Hibernate A Developer's Notebook" source code and the Hibernate Tools/IDE that is a plugin into eclipse. I can successfully run code that returns a Track along with its relationship to Artists...(a many to many). If I try to run the HQL query that is produced from the hibernate.show_sql within the Hibernate IDE...it fails. How would I query a Track for its name and all the Artists names using HQL? Track has a many to many relationship to Artists and Artist has a many to many relationship with Track.

I tried writing something like
Code:
select track.title, track.artists.name from Track as track


And this returns:
Code:
expecting 'elements' or 'indices' after: name [select track.title, track.artists.name from com.oreilly.hh.Track as track]


Artist.hbm.xml
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>

  <class name="com.oreilly.hh.Artist" table="ARTIST">
    <meta attribute="class-description">
      Represents an artist who is associated with a track or album.
      @author Jim Elliott (with help from Hibernate)
    </meta>

    <id name="id" type="int" column="ARTIST_ID">
      <meta attribute="scope-set">protected</meta>
      <generator class="native"/>
    </id>

    <property name="name" type="string">
      <meta attribute="use-in-tostring">true</meta>
      <column name="NAME" not-null="true" unique="true" index="ARTIST_NAME"/>
    </property>

    <set name="tracks" table="TRACK_ARTISTS" inverse="true">
      <meta attribute="field-description">Tracks by this artist</meta>
      <key column="ARTIST_ID"/>
      <many-to-many class="com.oreilly.hh.Track" column="TRACK_ID"/>
    </set>

  </class>

  <query name="com.oreilly.hh.artistByName">
    <![CDATA[
        from com.oreilly.hh.Artist as artist
        where upper(artist.name) = upper(:name)
      ]]>
  </query>

</hibernate-mapping>

Track.hbm.xml
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>

  <class name="com.oreilly.hh.Track" table="TRACK">
    <meta attribute="class-description">
      Represents a single playable track in the music database.
      @author Jim Elliott (with help from Hibernate)
    </meta>

    <id name="id" type="int" column="TRACK_ID">
      <meta attribute="scope-set">protected</meta>
      <generator class="native"/>
    </id>

    <property name="title" type="string">
      <meta attribute="use-in-tostring">true</meta>
      <column name="TITLE" not-null="true" index="TRACK_TITLE"/>
    </property>

    <property name="filePath" type="string" not-null="true"/>

    <property name="playTime" type="time">
      <meta attribute="field-description">Playing time</meta>
    </property>

    <set name="artists" table="TRACK_ARTISTS">
      <key column="TRACK_ID"/>
      <many-to-many class="com.oreilly.hh.Artist" column="ARTIST_ID"/>
    </set>

    <set name="comments" table="TRACK_COMMENTS">
      <key column="TRACK_ID"/>
      <element column="COMMENT" type="string"/>
    </set>

    <property name="added" type="date">
      <meta attribute="field-description">When the track was created</meta>
    </property>

    <property name="volume" type="short" not-null="true">
      <meta attribute="field-description">How loud to play the track</meta>
    </property>

  </class>

  <query name="com.oreilly.hh.tracksNoLongerThan">
    <![CDATA[
        from com.oreilly.hh.Track as track
        where track.playTime <= :length
      ]]>
  </query>

</hibernate-mapping>



Thanks,

-jay


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 17, 2005 10:17 am 
Newbie

Joined: Tue Feb 01, 2005 8:18 am
Posts: 8
try something like

select track.title, track.artists.elements.name from Track as track

in HibernateIde


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.