-->
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.  [ 7 posts ] 
Author Message
 Post subject: having problem with a self-recursive class hierarchy
PostPosted: Mon Aug 31, 2009 2:41 pm 
Newbie

Joined: Wed Aug 12, 2009 10:45 am
Posts: 16
Hi everyone;
I have a class hierarchy that is a little bit confusing. I tried to hunt through the examples but could not find anything that is quite similar;
My super class is like this:
Code:
public abstract class Product {
   
   private long id;
   //rest of properties
   

}


One of its subclasses is straightforward.I used joined-subclass tag and added some properties:
Code:
public class Print extends Product {
   
   //some extra properties

}


but this subclass has a reference to a list of super-class references!



Code:

//this class has no extra properties. It only adds the functionality that a product can have other products in it to be treated as a bundle.
public class Bundle extends Product {
   
   List<Product> porducts;

}




I can't figure out how to do this mapping especially that the bundle has a many to many relationship to its super-class, that is to say, any product can be part of many bundles and any bundle (quite obviously) can have many products in it.

Any ideas? I REALLY appreciate it.
Thanks
Kamal


Last edited by kamalsofteng on Mon Aug 31, 2009 5:19 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: having problem with a class hierarchy example
PostPosted: Mon Aug 31, 2009 2:58 pm 
Newbie

Joined: Fri Aug 28, 2009 12:50 pm
Posts: 5
My team has a similar relationship where a entity type contains a collection of other entities of the same type. I don't recall this requiring any special handing. Just treat the collection of entities like it is any other class. I believe this would work:

Code:
<hibernate-mapping>
    <class name="Product">
...

        <subclass name="Bundle">
...
        <list name="products">

            <!-- key column is the foreign ref back to the parent Bundle -->
            <key column="bundle_id_col"/>

            <!-- this is the index within the list for a particular product -->
            <list-index column="list_position_col"/>

            <one-to-many class="Product"/>
        </list>
...


Top
 Profile  
 
 Post subject: Re: having problem with a class hierarchy example
PostPosted: Mon Aug 31, 2009 4:27 pm 
Newbie

Joined: Wed Aug 12, 2009 10:45 am
Posts: 16
barnesjd wrote:
My team has a similar relationship where a entity type contains a collection of other entities of the same type. I don't recall this requiring any special handing. Just treat the collection of entities like it is any other class. I believe this would work:

Code:
<hibernate-mapping>
    <class name="Product">
...

        <subclass name="Bundle">
...
        <list name="products">

            <!-- key column is the foreign ref back to the parent Bundle -->
            <key column="bundle_id_col"/>

            <!-- this is the index within the list for a particular product -->
            <list-index column="list_position_col"/>

            <one-to-many class="Product"/>
        </list>
...




Thanks for the note.
My problem is that I am inside a subclass mapping. Assume you have product abstract and in one if its implementations classes, you have that collection!
I have to create a join element of the subclass but don't know how to make the many-many join table.
The subclass does not have any other properties, but a list of parent class objects.


Top
 Profile  
 
 Post subject: Re: having problem with a class hierarchy example
PostPosted: Mon Aug 31, 2009 5:25 pm 
Newbie

Joined: Fri Aug 28, 2009 12:50 pm
Posts: 5
kamalsofteng wrote:
Thanks for the note.
My problem is that I am inside a subclass mapping. Assume you have product abstract and in one if its implementations classes, you have that collection!
I have to create a join element of the subclass but don't know how to make the many-many join table.
The subclass does not have any other properties, but a list of parent class objects.


Take a second look at my previous post. I showed a mapping of the class Product with Bundle as a subclass. The class of the objects contained in your collection only needs to be mapped. The relationship of that class to the class containing the collection is immaterial.

On the other hand, you would have an issue with my mapping because I declared the list as one-to-many. I believe if you just change it to many-to-many it should just work. If it doesn't, post the error so I have a better idea of why you're having problems.

Joe


Top
 Profile  
 
 Post subject: Re: having problem with a class hierarchy example
PostPosted: Mon Aug 31, 2009 6:01 pm 
Newbie

Joined: Wed Aug 12, 2009 10:45 am
Posts: 16
barnesjd wrote:
kamalsofteng wrote:
Thanks for the note.
My problem is that I am inside a subclass mapping. Assume you have product abstract and in one if its implementations classes, you have that collection!
I have to create a join element of the subclass but don't know how to make the many-many join table.
The subclass does not have any other properties, but a list of parent class objects.


Take a second look at my previous post. I showed a mapping of the class Product with Bundle as a subclass. The class of the objects contained in your collection only needs to be mapped. The relationship of that class to the class containing the collection is immaterial.

On the other hand, you would have an issue with my mapping because I declared the list as one-to-many. I believe if you just change it to many-to-many it should just work. If it doesn't, post the error so I have a better idea of why you're having problems.

Joe



Thanks Joe;
It seems like I am getting almost there. I went back to my classes and after a few back and forth, I changed the xml file to look like this:


Code:
<?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 Aug 31, 2009 1:40:56 PM by Hibernate Tools 3.2.5.Beta -->
<hibernate-mapping default-access="field">
    <class name="test.ProductDefinition"  table="PRODUCT_DEFINITIONS"  abstract="true">
        <id name="id">
            <column name="id" />
            <generator class="native" />
        </id>
       
 
       
        <property name="description" type="string">
            <column name="DESCRIPTION" />
        </property>
        <property name="orderMultiple" type="int">
            <column name="ORDER_MULTIPLE" />
        </property>
        <property name="price" type="float">
            <column name="PRICE" />
        </property>
       
        <property name="billingID" type="string">
            <column name="BILLING_ID" />
        </property>
       
         
       
       
       
       
           
       
        <subclass name="test.Print" discriminator-value="PRINT" >
           <join table="PRINTS">
              <key column="PRINT_ID"></key>
              <property name="width" type="float" column="width"></property>
              <property name="height" type="float" column="height"></property>
            
         <property name="printLayoutID" column="print_layout_id" type="string"></property>
           </join>
           
           
           
         </subclass>
         
         
         <subclass name="test.Bundle" discriminator-value="BUNDLE" >
         
            
            
            <set name="products" table="BUNDLE_PRODUCTS">
               <key column="PARENT_ID"></key>
               <many-to-many class="test.ProductDefinition" column="CHILD_ID"></many-to-many>
               
            </set>         
            
         </subclass>
         

       
    </class>
</hibernate-mapping>



However, this time it gives me an error that does not seem to make sense:
Code:
org.hibernate.MappingException: No discriminator found for test.Print. Discriminator is needed when 'single-table-per-hierarchy' is used and a class has subclasses

And if you see, I have discriminator values for both subclasses!


Top
 Profile  
 
 Post subject: Re: having problem with a self-recursive class hierarchy
PostPosted: Tue Sep 01, 2009 9:33 am 
Newbie

Joined: Wed Aug 12, 2009 10:45 am
Posts: 16
Thanks all;
Got it to work. The problem was that I had to put the <discriminator column .... just after the id, the sequence seems to be mandated by the dtd. Even though, the discriminator is optional, but I had it in the wrong sequence.

Kamal


Top
 Profile  
 
 Post subject: Re: having problem with a self-recursive class hierarchy
PostPosted: Wed Oct 14, 2009 9:55 am 
Newbie

Joined: Mon Oct 12, 2009 6:07 am
Posts: 7
Hi,

Similar to your requirement, I need to define a list of entities inside join tag of subclass ..

ANy idea how to do this?

Thanks in advance!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.