hi,
I have 3 tables, TRACK, ARTIST and a table named TRACK_ARTISTS to link to the Track objects for which this Artist is responsible.
I have a class CreateTest which loads artist and track information to the 3 tables.
When I try to load information to the tables I get the below exception
Hibernate 2.1
Full stack trace of any exception that occurs: [java] Exception in thread "main" net.sf.hibernate.MappingException: An ass ociation from the table TRACK_ARTISTS refers to an unmapped class: com.oreilly.h h.Artist [java] at net.sf.hibernate.cfg.Configuration.secondPassCompileForeignKe ys(Configuration.java:696) [java] at net.sf.hibernate.cfg.Configuration.secondPassCompile(Configur ation.java:680) [java] at net.sf.hibernate.cfg.Configuration.buildSessionFactory(Config uration.java:798) [java] at com.oreilly.hh.CreateTest.main(CreateTest.java:24) [java] Java Result: 1
Name and version of the database you are using: hsqldb
Track.hbm.xml Mapping documents:<?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>
<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>
Artist.hbm.xml <?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>
CreateTest.java package com.oreilly.hh;
import net.sf.hibernate.*; import net.sf.hibernate.cfg.Configuration;
import java.sql.Time; import java.util.*;
/** * Create more sample data, letting Hibernate persist it for us. */ public class CreateTest {
/** * Look up an artist record given a name. * @param name the name of the artist desired. * @param create controls whether a new record should be created if * the specified artist is not yet in the database. * @param session the Hibernate session that can retrieve data * @return the artist with the specified name, or <code>null</code> if no * such artist exists and <code>create</code> is <code>false</code>. * @throws HibernateException if there is a problem. */ public static Artist getArtist(String name, boolean create, Session session) throws HibernateException { Query query = session.getNamedQuery( "com.oreilly.hh.artistByName"); query.setString("name", name); Artist found = (Artist)query.uniqueResult(); if (found == null && create) { found = new Artist(name, new HashSet()); session.save(found); } return found; }
/** * Utility method to associate an artist with a track */ private static void addTrackArtist(Track track, Artist artist) { track.getArtists().add(artist); }
public static void main(String args[]) throws Exception { // Create a configuration based on the properties file we've put // in the standard place. Configuration config = new Configuration();
// Tell it about the classes we want mapped, taking advantage of // the way we've named their mapping documents. config.addClass(Track.class).addClass(Artist.class);
// Get the session factory we can use for persistence SessionFactory sessionFactory = config.buildSessionFactory();
// Ask for a session using the JDBC information we've configured Session session = sessionFactory.openSession(); Transaction tx = null; try { // Create some data and persist it tx = session.beginTransaction();
Track track = new Track("Russian Trance", "vol2/album610/track02.mp3", Time.valueOf("00:03:30"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("PPK", true, session)); session.save(track);
track = new Track("Video Killed the Radio Star", "vol2/album611/track12.mp3", Time.valueOf("00:03:49"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("The Buggles", true, session)); session.save(track);
track = new Track("Gravity's Angel", "vol2/album175/track03.mp3", Time.valueOf("00:06:06"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("Laurie Anderson", true, session)); session.save(track);
track = new Track("Adagio for Strings (Ferry Corsten Remix)", "vol2/album972/track01.mp3", Time.valueOf("00:06:35"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("William Orbit", true, session)); addTrackArtist(track, getArtist("Ferry Corsten", true, session)); addTrackArtist(track, getArtist("Samuel Barber", true, session)); session.save(track);
track = new Track("Adagio for Strings (ATB Remix)", "vol2/album972/track02.mp3", Time.valueOf("00:07:39"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("William Orbit", true, session)); addTrackArtist(track, getArtist("ATB", true, session)); addTrackArtist(track, getArtist("Samuel Barber", true, session)); session.save(track);
track = new Track("The World '99", "vol2/singles/pvw99.mp3", Time.valueOf("00:07:05"), new Date(), (short)0, new HashSet(), new HashSet()); addTrackArtist(track, getArtist("Pulp Victim", true, session)); addTrackArtist(track, getArtist("Ferry Corsten", true, session)); session.save(track);
track = new Track("Test Tone 1", "vol2/singles/test01.mp3", Time.valueOf("00:00:10"), new Date(), (short)0, new HashSet(), new HashSet()); track.getComments().add("Pink noise to test equalization"); session.save(track);
// We're done; make our changes permanent tx.commit();
} catch (Exception e) { if (tx != null) { // Something went wrong; discard all partial changes tx.rollback(); } throw e; } finally { // No matter what, close the session session.close(); }
// Clean up after ourselves sessionFactory.close(); } }
Appreciate all help.
thanks in advance
|