Browse DevX
Sign up for e-mail newsletters from DevX


Object Generation: A Better Approach to Hibernate Integration : Page 5

If you're using Hibernate, your business objects may be littered with unnecessary code. Learn a different, proven approach that challenges conventional wisdom and promises more streamlined integration with less code.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

The Inevitable Limitations

Every approach to persistence has its limitations, and object generation is no exception. Its list of limitations, however, is manageable:
  1. Surrogate keys must be used for all persistent entities. These keys uniquely identify objects and are the simple foundation for the approach. Fortunately, the use of surrogate keys is a commonly recommended best practice used in many modern enterprise applications.
  2. Certain identifier generators are not supported. More specifically, any PostInsertIdentifierGenerator is not supported. The most common example is the "identity" generator used to support SQLServer identity columns. Object generation requires that an object's identifier be allocated before the object's persisted. This issue shouldn't be a showstopper, though, as numerous viable workarounds exist, including the TableHiLoGenerator supplied with Hibernate.
  3. A Hibernate Session must exist when creating new objects. Remember, identifiers are allocated at creation, and this process often requires a call into the database. If you're working on a Web application, this is commonly addressed by wrapping each request in an open session (take a look at the OpenSessionInViewFilter class packaged with the Spring framework for a good example).

Finally, in advanced use cases, some users need to detach objects from one session only to later associate them with a different session. This is commonly done to implement application or conversational transactions, and object generation easily accommodates it.

A More Streamlined Hibernate Experience

Object generation is an alternative Hibernate integration approach based on practical experience with the framework and its use in enterprise applications. The approach delivers on its promise of simplifying persistence by reducing the code you're left to design, write, and maintain. Gone is the burden of identifying or inventing business keys and implementing customized hashCode and equals methods throughout. Instead, you use surrogate keys as identifiers and supply the supporting infrastructure, including some runtime class enhancement, to make it all work. The result is a more streamlined Hibernate experience that allows even more focus where it counts—on your business problem.

Lance Arlaus is President of Codeberet, a software consultancy that specializes in intense engagements across a variety of vertical industries. His contribution is solving tough problems raised by real world projects with equal parts technical proficiency and management acumen.
Thanks for your registration, follow us on our social networks to keep up-to-date