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>