Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.1.2
Eclipse version: 3.1
Mapping documents:
Track.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 3, 2006 12:30:55 AM by Hibernate Tools 3.1.0.beta4 -->
<hibernate-mapping package="chapter2">
<class name="Track" table="TRACK">
<id name="id" type="integer">
<column name="Track_ID" />
<generator class="native"></generator>
</id>
<property name="title" type="string">
<column name="TITLE" not-null="true" index="TRACK_TITLE"/>
</property>
<property name="filePath" type="string">
<column name="filePath" not-null="true" />
</property>
<property name="playTime" type="time">
<column name="playTime" />
</property>
<set name="artists" table="TRACK_ARTISTS">
<key column="TRACK_ID" />
<many-to-many class="Artist"
column="ARTIST_ID" />
</set>
<property name="added" type="date">
<column name="added" />
</property>
<property name="volume" type="short">
<column name="volume" />
</property>
</class>
</hibernate-mapping>
Artist.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 3, 2006 12:30:54 AM by Hibernate Tools 3.1.0.beta4 -->
<hibernate-mapping package="chapter2">
<class name="Artist" table="ARTIST">
<id name="id" type="integer">
<column name="ARTIST_ID" />
<generator class="native"></generator>
</id>
<property name="name" type="string">
<column name="NAME" not-null="true" unique="true" />
</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="Track" column="TRACK_ID" />
</set>
<query name="chapter2.artistByName">
<![CDATA[
from Artist as artist
where upper(artist.name) = upper(:name)
]]>
</query>
</class>
</hibernate-mapping>
Name and version of the database you are using: hsqldb 1.8
hsqldb-hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:file:build/data/canhac;hsqldb.default_table_type=cached;shutdown=true</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<mapping resource="chapter2/Track.hbm.xml"/>
<mapping resource="chapter2/Artist.hbm.xml"/>
</session-factory>
</hibernate-configuration>
********************************
code to build session factory :
File f = new File("F:/ZZZ/hsqldb-hibernate.cfg.xml");
Configuration config = new Configuration();
config.configure(f);
//Tell it about classes we want mapped if we did not
//declare in the .cfg.xml file
config.addClass(Artist.class);
//Get the session factory we can use for persistence
SessionFactory sessionFactory = config.buildSessionFactory();
The problem is: without the line config.addClass(Artist.class), i get the mapping exception (an association table refer ...unknown mapping class ...) . Why ? I declared mapping Artist in file hsqldb-hibernate.cfg.xml . Why i need to declare here once more ? But here, if i declare for Track class , like config.addClass(Track.class) , i get a duplicate mapping exception ( certainly, i understand) . But how about Artist , what wrong here ? Please help me .Thank you !