Archive for May, 2009

Bidirectional Associations and JiBX

I recently needed to use JiBX to parse a XML document into domain objects that contained bidirectional associations with collections.  The issue started out as a simple NullPointerException since each child object did not have a reference back to its parent. 

After some searching on Google, the only suggestions that I came across had to do with using the post-set attribute, which gets called after the class has been unmarshalled.  For various reasons, most of which I did not want the domain classes tied to JiBX, I sought out a different solution.

The solution I came up with was to use a set-method for the collection instead of field.  So I replaced field=”fieldName” with set-method=”setFieldName”.  This set method ensures that the bidirectional association is properly defined. 

There is an argument to be made that I should be doing this anyway.  After all, the domain class probably should not let it be up to the caller to ensure the bidirectional association is properly defined, regardless if it is being called from JiBX or some other caller.

However there was one unintentional side affect.  The project is also using Hibernate.  The same domain classes were mapped using lazy associations.  This change to the set method in affect turned off the laziness.  The solution for this was to change the Hibernate mappings to use field access to bypass the set method.

New Unidev Microsoft .NET Technology Blog

We’ve added a new .NET technology blog that is specifically focused on Microsoft .NET development and Microsoft SQL Server. This blog is written my the various developers from our .NET team and will cover a broad range of topical technical issues. We hope you find articles like the recent one about IE 8 connectivity issues useful and a good read. Please feel free to offer us feedback as we’ll certainly need it.

Paging the query result

If you have 5000 products in database, you may like to show them in pages on the screen. It’s a common request to only retrieve the records on a certain page. This query must be quick and efficient if we are talking about millions of records in a table for an AJAX control.

In Oracle, we can do this:

SELECT * from
(
SELECT *, ROWNUM as rownumber from Product order by name
)
where rownumber between (PageNbr - 1)*PageSize + 1 and PageNbr*PageSize

As you can see, this query needs to fetch all rows first then narrows down the result. A more efficient query is:

select *
from ( select a.*, rownum rnum
from ( select * from product order by name) a
where rownum <= PageNbr*PageSize)
where rnum >= (PageNbr - 1)*PageSize + 1

Oracle will optimize this query, so it is not as resource intensive as the first one.

In SQL Server 2005, we can do similar query:

WITH Temp AS
(
SELECT row_number() OVER (ORDER BY [name]) AS rowNum, * from Product
)
SELECT * FROM Temp
WHERE rowNum between (@PageNbr - 1) * @PageSize + 1 and @PageNbr * @PageSize
ORDER BY [name]

The query had been optimized in SQL Server as well.