I have been looking for the answer to a question but have yet to find it so I figure I'll post before I'm up all night (hate not solving problems)...
With Hibernate or JPA we have lots of options for modeling inheritance within the domain model. We can have a base class that holds shared properties and sub-classes for unique properties.
I've been struggling with a good way to represent this in
JSF and reusing the
Java classes already designed for the domain layer.
As example (with pseudo-code simplifications):
class Payment {
id;
date;
amount;
}
class CreaditPayment extends Payment {
creditCardNumber;
}
class CheckingPayment extends Payment {
bankRouting;
}
With Hibernate/JPA we can represent this by using an inheritance strategy that stores what the actual "type" of the domain object is in a discriminator column. The problem that will pop up later is this is like a "pseudo property" since it is not actually in any of the Java classes (no get/set). This column exists in the database to tell Hibernate/JPA which type of object
instance to create in the object model.
The problem in JSF appears when I try to display these different elements in something like a table. I'd like to use a single table to show all "payments" and simply have columns filled in if the payment instance has that type of property. This means there will be JSF tags like this:
<h: outputText value=#{payment.creditCardNumber}"/>
<h: outputText value=#{payment.bankRouting}"/>
The problem is when I try this I get JspPropertyNotFoundException which of course makes perfect sense because the instances won't have the Java properties of the other sub-class.
I need some way to conditionaly evaluate the expression based on the type of instance or tell JSF to skip this altogether. Unfortunately, since the 'type' is not an actual Java property I'm not sure how to do this.
I won't consider creating a DTO class that has all possible properties because that is just a hack that doesn't deserve recognition. There are sub-classes for a reason.
There could be something extremely simple that I'm missing here. If that is the case I will truly be glad to say "how could I be so dumb?" after the problem is solved.
I think part of the problem is that since WebSphere and
JBoss only recently came out with Java EE 5 implementations we have not as an industry really explored the question "How do we update our design
patterns to account for the new technologies (JSF, JPA,
EJB 3)?". Hopefully questions like this can help us do that (and give a Google hit to the next poor sap that is trying to solve this problem at 5AM

)
Thanks,
Stuart Smith