I'm using Hibernate 3.2.1 with Java5 Generics.
My business model looks like this:
Code:
public class Contract<InvoiceType extends Invoice> {
// ...
private InvoiceType invoice;
public InvoiceType getInvoice() {
return invoice;
}
public void setInvoice(InvoiceType invoice) {
this.invoice = invoice;
}
}
Code:
public class SpecialContract extends Contract<SpecialInvoice> {
// ...
}
Code:
public class SpecialInvoice extends Invoice {
// ...
}
Persisting all this stuff works well, but when I have a persistant instance of SpecialContract, call getInvoide() and assign the result to a SpecialInvoice variable, I get a ClassCastException. The following code snippet illustrates it:
Code:
public class Test {
public static void main(String[] args) throws Exception {
Session session = HibernateUtil.getSession();
SpecialContract contract = (SpecialContract) session.load(SpecialContract.class, new Long(1)); // retrives the special contract - this works
SpecialInvoice invoice = contract.getInvoice(); // this works well in case of non-persistent code, but throws a ClassCastException when using Hibernate
HibernateUtil.closeSession();
}
}
That's the exception:
Code:
Exception in thread "main" java.lang.ClassCastException: packagename.Invoice$$EnhancerByCGLIB$$5b598911
at packagename.Test.main
For completeness, here's the corresponding mapping:
Code:
<hibernate-mapping package="packagename">
<class name="Contract" discriminator-value="C">
...
<many-to-one name="invoice" cascade="all" />
</hibernate-mapping>
Isn't it possible to persist such generic fields using Hibernate or did I miss something in the configuration?
Thanks,
Thorsten[/code]