-->
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: one-to-many composition
PostPosted: Tue Dec 13, 2005 9:06 am 
Newbie

Joined: Tue Dec 13, 2005 8:02 am
Posts: 1
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.14

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:
Code:


Top
 Profile  
 
 Post subject: One-To-Many
PostPosted: Tue Dec 13, 2005 9:54 am 
Beginner
Beginner

Joined: Wed Apr 21, 2004 8:33 am
Posts: 27
Hi,
I dont think you have to define an object of CD in the Track Class. Can you post the exception that is being thrown when you dont define the same.

Tables : A > B

Want to have One - Many from A > B

Define One-Many Mapping in A.xml , and the corresponding Set Definition in the Value Object.

Similarly Many to One from B > A
Define Many-to-One in B.xml and the corresponding Object Definiton in Value Object.

You need not necessarly have both at the same time

Cheers
Vijay


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.