Strategies for Getting Started
Throughout this text, I've tried to provide as balanced a set of recommendations as possible. In this section, I offer some general guidelines for those who are still unsure about the proper approach. Consider this section to reflect opinion onlyand keep in mind that since I don't know your application's requirements, these recommendations should be taken with a grain of salt.
Where to Start?
My approach for working with Hibernate depends on whether or not there is an existing database. If you are starting with an existing database, use Middlegen (as described in Chapter 4). If the database schema is extremely stable, you may wish to use Middlegen to generate a first pass at the *.hbm.xml files, and then hand-tune them as needed. If you believe the schema is potentially subject to change, follow the guidelines for Middlegen as described in Chapter 4.
If you are starting from scratch, use the *.hbm.xml mapping as your canonical format, and generate both your Java and database schema based on that (as described in Chapter 2 and Chapter 11). I find that starting from Java (as described in Chapter 3) is most comfortable for developers who are accustomed to using XDoclet as a refuge from the complexity of EJB. I find it much easier to generate persistent objects from my mapping file than to rely on generating get/set methods in bulk and then inspecting the generated *.hbm.xml files.
Start with Many-to-One and One-to-Many
Make sure that you understand the many-to-one and one-to-many relationships before working on the more esoteric types shown in Chapter 7. The vast majority of databases can be modeled relatively conveniently using only these two basic relationships. Class and the more sophisticated relationships are useful tools, but you should feel comfortable with the basic relationships before tackling them.
Profile Database Fetching
When you are first starting out with Hibernate, the relationship between queries and lazy loading of objects can be confusing. You will probably find it especially difficult to understand the implications of the different data-loading strategies. Use the tools shown in Chapter 10 to inspect and time the SQL generated by Hibernate.
Put another way, make sure that you understand how your object graph is going to be traversed. How do you expect to access your data? Make sure that you understand as many as possible of the creation, retrieval, update, and delete (CRUD) operations on your objects.
|