-->
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: How to get the underlying column type for a Set?
PostPosted: Thu Dec 11, 2003 12:45 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 10:05 am
Posts: 50
Location: Dublin, Ireland
How do I get the underlying type of the field in the following example?

What I'm currently doing is something like this (condensed version)

Code:
Class clazz = //the Portfolio class object
EntityPersister classMetadata = ((EntityPersister)sessionFactory.getClassMetadata(clazz));
Type [] propertyTypes = classMetadata.getPropertyTypes();
String [] propertyNames = classMetadata.getPropertyNames();
Type columnType;


then I match the column name I'm interested in ("portfoliotocustomers") against the propertyNames array, and pick up the equivalent "Type" entry from "propertyTypes" into "columnType"
columnType = blah();

Code:
Class nestedClass = columnType.getReturnedClass();


Now for the basic columns (where the columns are not one-to-many), this returns the correct type (because the underlying type in the entity itself is returned, which is perfect for my purposes), but for a <one-to-many> type column represented in my case by a java.util.Set, I get returned a Set class, when in fact I would like to get hold of a "PortfolioToCustomer" class (as per the mapping file entry)

I suppose I could get the first Set entry, and use the class of that, but what if the Set is empty??? (Which, in my case, it will be, at least initially). I presume theres a way to do this using the Hibernate API.. I just haven't encountered it yet...

Any ideas (see code below)?

Java code

Code:
public class Portfolio extends Blah
{
private Integer portfolioid; //primary key for Portfolio
private Set portfoliotocustomers; //one-to-many from portfolio to a table called "portfoliotocustomers", each row of which is a relationship between a portfoli oand a customer

   public void setPortfoliotocustomers ( Set portfoliotocustomers)
   {
   this.portfoliotocustomers = portfoliotocustomers;
   }
   
   public Set getPortfoliotocustomers ()
   {
   return this.portfoliotocustomers;
   }
}


Mapping fragment

Code:
<set
            name="portfoliotocustomers"
            lazy="false"
            inverse="false"
            cascade="none"
            sort="unsorted"           
        >

              <key
                  column="portfolioid"
              />

              <one-to-many
                  class="com.kbcam.core.entity.PortfolioToCustomer"
              />
        </set>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 12:56 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
take a look at getCollectionMetadata


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 1:36 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 10:05 am
Posts: 50
Location: Dublin, Ireland
Do u mean the following (in which case, I need to set a role name in my mapping file, which is something I haven't been doing until now)?
Code:
CollectionMetadata cmd = sessionFactory.getCollectionMetadata ("portfoliotocustomers");
    if (cmd !=null)
        {
        Class c = cmd.getClass();
        if (c!= null)
            log.trace ("Class is "+ c.getName());
        }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 1:39 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I have never tried to work with getCollectionMetadata :)

I suppose you should use it like

Code:
getCollectionMetadata("Portfolio.portfoliotocustomers");


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 1:42 pm 
Regular
Regular

Joined: Mon Sep 08, 2003 10:05 am
Posts: 50
Location: Dublin, Ireland
Will bang a couple of tests at it.. will post back with whatever works..

As a by-note.. I'm not sure that XDoclet supports setting the role attribute on a Set... will check that as well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 1:45 pm 
Expert
Expert

Joined: Tue Sep 16, 2003 4:06 pm
Posts: 318
Location: St. Petersburg, Russia
I think all collections has rolename - it is classname.collectionname


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2003 6:27 am 
Regular
Regular

Joined: Mon Sep 08, 2003 10:05 am
Posts: 50
Location: Dublin, Ireland
Making Progress.. the following returns me the PortfolioToCustomer class,

(where I am looking for the type of the following: "portfoliotocustomers.customer")

CollectionMetadata cmd = sessionFactory.getCollectionMetadata ("com.kbcam.core.entity.Portfolio.portfoliotocustomers");
Type prefixColType =cmd.getElementType();
Class prefixclass = prefixColType.getReturnedClass();

then I need to recurse to get the type of "customer", using the prefixclass class as the basis.. this then returns the correct class: "Customer".. so i am in business! Many thanks for the info.

To clear up the loose ends:

    1) the mapping I am using does NOT contain a "role='blah'" mapping (its the same as in my previous post)
    2) XDoclet 1.2beta3 does not support the role="blah" attribute on @set (@set is still supported though). Note that this DOES seem to be supported in XDoclet 1.2-beta2 though.. although I see no mention of deprecation anywhere.


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.