The Inevitable Limitations
Every approach to persistence has its limitations, and object generation is no exception. Its list of limitations, however, is manageable:
- 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.
- 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.
- 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 countson your business problem.