Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hi everybody,
I am new to Hibernate and as a matter of fact I am using it for my dissertation project. I have already successfully implemented: associations (bidirectional and many-to-many) and inheritance but I am not that clear on one-to-many composition mapping. My application is the cd store and part of it is composition between the CD class and the Track class since a CD has many tracks. The CD class defines:
Code:
package cdstore;
import java.util.Set;
import java.util.HashSet;
public class CD {
protected int id;
protected String cdTitle;
protected String releaseDate;
protected String label;
protected String format;
protected Act act;
protected Set<Track> tracks = new HashSet<Track>(); // Composition
.
.
.
}
and the Track class defines:
Code:
package cdstore;
public class Track {
private int id;
private String trackTitle;
private CD cd;
.
.
.
}
Furthermore part of the database schema defines the cd and track tables in a one-to-many relationship where the cdID is a foreign key column in the track table.
In my first cut design there was no CD object field in the Track class and Hibernate was throwing exceptions left and right. Once I included the object field everything goes smoothly and the client displays the CD details and lists the tracks.
My question is probably naïve but I really want to know whether I mapped composition or a one-to-many association? My intention is to map one-to-many composition so did I implement it correctly?
Many thanks,
Nick.
Hibernate version: 3.0.5 Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- Inheritance mapping strategy: One table per class hierarchy -->
<class name="cdstore.CD" table="cd">
<!-- Parent class mapping -->
<id name="id" type="integer" column="cdID">
<generator class="native"/>
</id>
<!-- Value identifying the type of the child class -->
<discriminator column="format" type="string"/>
<property name="cdTitle" type="string"
column="title" not-null="true"/>
<property name="releaseDate" type="string"
column="released" not-null="true"/>
<property name="label" type="string"
column="label" not-null="true"/>
<!-- Map association to relationship -->
<many-to-one name="act" class="cdstore.Act" column="actID" not-null="true"/>
<!-- Composition mapping to 1:M relationship -->
<set name="tracks" table="track" inverse="true">
<key column="cdID"/>
<one-to-many class="cdstore.Track"/>
</set>
<!-- Subclass mapping -->
<subclass name="cdstore.CDsingle" discriminator-value="Single">
<property name="price" column="price"/>
</subclass>
<subclass name="cdstore.CDalbum" discriminator-value="Album">
<property name="price" column="price"/>
</subclass>
<subclass name="cdstore.DoubleCD" discriminator-value="Double">
<property name="price" column="price"/>
</subclass>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!-- -->
<class name="cdstore.Track" table="track">
<!-- -->
<id name="id" type="integer" column="trackNo">
<generator class="native"/>
</id>
<property name="trackTitle" type="string"
column="trackName" not-null="true"/>
<!-- -->
<many-to-one name="cd" class="cdstore.CD" column="cdID"/>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Full stack trace of any exception that occurs:Name and version of the database you are using: MySQL 4.1.14The generated SQL (show_sql=true):Debug level Hibernate log excerpt:Code: