Associations that retrieve no data

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Associations that retrieve no data

Guy Rouillier
We are using MyBatis as the data access layer for a large JSF facelet
web application.  Briefly, JSF (and facelets) retrieves data from Java
Beans using EL (expression language) notation.  For example,
#{myBean.myProperty} tells JSF to locate a Java Bean named myBean, and
invoke method getMyProperty() on it.  You can extend the notation as
necessary to get nested properties, but it is up to you to ensure that
none of the intermediate nodes are null.

Our data model has many associations and collections; I mostly use a
select= attribute on my associations because they are reusable.
Sometimes, the association doesn't retrieve any results.  I try to
handle that in the setter for the associated class like so:

   public void setNewProdOption(ProdOptionRef newProdOption) {
     if (newProdOption != null)
       this.newProdOption = newProdOption;
       this.newProdOption = ProdOptionRef.NONE;

However, I discovered (after much gnashing of teeth) that in the absence
of a result set from an association, the setter is simply not called.
Now that I think about it, that makes sense in one way; since you got no
results, you have no object to call the setter with.  For those who may
find this via search, I solved this problem by initializing the bean
field like so:

   private ProdOptionRef newProdOption = ProdOptionRef.NONE;

I'm wondering what others think of having an optional attribute on the
association that would direct MyBatis what to do with an empty result
set.  Options may be: do nothing, invoke setter with null, or create an
empty object using the default constructor and invoke the setter with
that.  This could be accomplished with a customer TypeHandler, but the
optional attribute on association would save that additional work.

Guy Rouillier