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.xmlCode:
<?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.xmlCode:
<?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