Talking about Object Relational Mapping (ORM) for Java, Hibernate comes to mind for almost every developer. Recently, I have opportunity to evaluate another ORM API – TopLink and want to share my impression of this product and compare it with Hibernate.
TopLink is an ORM API from Oracle and it is similar to Hibernate in term of concept. It is not free product but has a branched release called Eclipselink, which is open source and has most functionality of its commercial counterpart. When we pick up an API for a project, there are always trade offs that have positive or negative impacts on project quality, timeline and cost. Based on my testing, I would like to explain how the choice of TopLink vs. Hibernate will impact the project from my point of view. It is fair to say that except the most simple and basic application, ORM, such as Hibernate or TopLink, is a better choice than plain old JDBC. ORM provides a Java persistent layer mapping to relational database tables and give Java programmer a familiar set of classes to build on. You do not need to deal with SQL if you do not want to. You read and write data at object level instead of table, row or cell level.
But what you like to pick, Hibernate or TopLink? Well, majority would start with Hibernate because it is popular. A lot of people use Hibernate and they have good reasons to do so. Hibernate is an open source API you do not have to pay a red cent for and get tons of features for return. It works well with other open source software, API and framework, such as Spring, Eclipse, Google web tool kit. There are huge numbers of developer social network for Hibernate that you can ask questions and get you start on the right track. In my opinion, the support level is better than many paid service. And if you really want to have somebody to call in panic mode, you can get paid support as well. Because Hibernate is widely used, most bugs for its core functions are fixed and it is a stable and solid API. On the other hand, there are all kinds of API extensions and products based Hibernate from open source and third party vendors. Anything you wish to have, likely someone had the same pain and released a fix for it already. Plus Hibernate is good to put on you resume and make you more marketable. Another feature I like about Hibernate is Hibernate dialect. With Hibernate dialect, The SQL generated is comparable with the SQL engine of the database platform configured and make it possible for the application using Hibernate to be deployed to multiple database platform without code change.
So what TopLink can offer and Hibernate is not so good at it? First, if you use Oracle database, you may want to consider TopLink. Developer can leverage TopLink Workbench GUI tool to work seamless with Oracle database on whole life cycle of ORM, namely, generating table from xml description file or generating from xml description file to table, and generate Java class from xml description file. Hibernate has many tools for configuration but they are not as easy to use as TopLink workbench on Oracle database. Secondly, if you tried of getting LayzyInitializatonException from Hibernate, you may want to try TopLink. TopLink session is persistent as well. (Not like Hibernate, you have to new a session before use it and close it after the use.) In TopLink, you can call session without initialization. TopLink manages database connection or pooling for you so you do not have to worry about it. It is especially useful for lazy loading of child objects. The third point I like is TopLink transaction management. TopLink uses UnitOfWork as transaction container. When an object is registered with UnitOfWork, it creates a clone of such object and leave original object unchanged. Once the transaction is completed, the original object is updated with the clone. If transaction failed, the clone is destroyed and no change to original object in cache. In Hibernate, the change in transaction is done on cached object directly and if the transaction failed, it is developer’s responsibility to rollback the state of modified cache object. Secondly, if there are more than one transactions going on in a session, it is impossible for Hibernate to handle because it can not isolate the changes within the transaction. Unlike Hibernate, which rely on application server or JDBC for transaction management, TopLink comes with its own transaction management, such as cache version based optimistic locking and timestamp based optimistic locking. Developer can setup locking method for each persistence Java class. That is a great feature for developing high performance application based on the data read and write pattern of the application. For TopLink, if a transaction contains both parent objects and children objects, it can write to database of such objects in the right order to avoid Oracle parent record not found error for insert and child record exist error for deletion.
Toplink is a good API for Java based database application in general. But new comer need pay attention to some areas. One of them is cache. You can configure TopLink cache at session level, or application server level. Only newest version of cache is accessible in general for the scope of caching and it may not be the same data as in database. So it is recommended to only read the cached object but never to change it unless you register it into UnitOfWork. Do not assume the cached object is the same as that in database. Always refresh it if you need it synchronized with database.
No comments:
Post a Comment