Hibernate, an object/relational mapping framework for Java applications. Hibernate provides the bridge between the data-base and the application by storing application objects in the database for the developer, rather than requiring the developer to write and maintain mountains of code to store and retrieve objects.
Hibernate is a solid, productive Object Relational Mapping (ORM) tool that lets developers think and work with objects rather than tables and columns.
Developers don’t work with Hibernate in a vacuum. In addition to stan-dard Java, developers often use Hibernate with a host of other third-party (often open source) tools and libraries, including J2EE (web applica-tions); build tools like Ant; unit-testing frameworks like JUnit; and frame-works like XDoclet, Struts, WebWork, Tapestry, and Spring.
Hibernate is a solid, productive Object Relational Mapping (ORM) tool that lets developers think and work with objects rather than tables and columns.
Developers don’t work with Hibernate in a vacuum. In addition to stan-dard Java, developers often use Hibernate with a host of other third-party (often open source) tools and libraries, including J2EE (web applica-tions); build tools like Ant; unit-testing frameworks like JUnit; and frame-works like XDoclet, Struts, WebWork, Tapestry, and Spring.
- How to generate your database from Hibernate mappings using the SchemaExport tool ? (discuss later)
Hibernate allows you to use basic java.util collections classes to express both one-to-many and many-to-many relationships between entities.
- How and why you can use Hibernate’s custom types, which let you define new datatypes that can map to database columns ? (discuss later).
- Hibern8IDE, a tool that lets you rapidly test and try your queries.
- Few patterns like the Data Access Object (DAO) and Layer Super types.
- In addition, we explain how to generate your configuration files, hibernate.cfg.xml, using XDoclet.
- JUnit and DBUnit, to verify that your Hibernate application works as expected.
- When refer to persistence in this book, we’re referring to storing application data in a relational database.
The model used by relational databases, called the relational model, represents data in two-dimensional tables.
However, when you’re working with object-oriented applications, you may encounter a problem when you try to persist objects to a relational model.
Resolving the avbove two model differences has been the subject of much debate over the years and has been referred to as the object/relational impedance mismatch or just impedance mismatch.
If tables refer to each other through columns other than primary keys, your chosen ORM solution may have trouble adapting to the legacy schema.
Alternative solutions, such as iBATIS, may be a better candidate for persisting legacy databases. (iBATIS (www.ibatis.com) is an alternative to Hibernate.)
ORM is a good solution for legacy databases when the schema
- Is highly normalized
- Has primary keys
- Has foreign key relationships referring to primary keys, not col- umns
A domain model is the collection of related objects describing the problem domain for an application. Each object in the domain model represents some significant component in the application.
For example you have the JDBC resultset, Now that you have the results, how should you return the data to the application? There are two options: You can convert the results either into a list of Event objects or into a list of Map instances.
After examining the shortcomings in our persistence techniques using direct JDBC, let’s discuss the benefits of using Hibernate for application persistence.
The core benefits of Hibernate are simplicity, flexibility, completeness, and performance.
Rather than the handful of classes and configuration properties required by some persistence solutions, such as EJBs, Hibernate requires requires a single runtime configuration file and one mapping document for each application object to be persisted.
- The runtime configuration file can be a property file or an XML file. or can be done programmatically.
- Lazy collections provide another performance enhancement. Rather than load collections of objects when the parent object is loaded, collections are populated only when they’re accessed by the application.
- Let’s say you have a User class with a oneto-one association to a Department class. When you’re retrieving a User instance, you don’t always need the associated Department instance, so you can declare that the object should be retrieved only when needed by setting outer-join to false.
- It’s also possible to declare proxies for objects, resulting in the objects being populated only when accessed by the developer. When you declare a proxy, Hibernate creates an unpopulated representation of the persistent class. The proxy is replaced with the actual instance of the persistent class only when you access the object by calling one of its methods.
- Proxies are related to the outer-join setting we just mentioned. Usingthe same brief example, if the Department class is declared to have aproxy, you don’t need to set the outer-join from the User to theDepartment to false—the Hibernate service will only populate theDepartment instance when needed.
- Object caching also plays a large role in improving application performance. Hibernate supports various open-source and commercial caching products. Caching can be enabled for a persistent class or for acollection of persistent objects,
- Query results can also be cached, but doing so only benefitsqueries that run with the same parameters. Query caching doesn’t significantly benefit application performance, but the option is availablefor appropriate cases.
- By using Hibernate, you can avoid writing vendor-specificJDBC code.
- Hibernate works with various supportservices, such as connection pools, caching services, and transactionmanagers. It also lets you maintain additional support services byimplementing simple interfaces.
- Hibernate provides two alternative configuration files: a standard Javaproperties file called hibernate.properties and an XML formatted file called hibernate.cfg.xml.
- If both the hibernate.properties and hibernate.cfg.xml files are found in the application classpath, then hibernate.cfg.xml overrides the settings found in the hibernate.properties file.
- Database connections may be provided by the Hibernate framework or from a JNDI Data Source. Athird method, user-provided JDBC connections, is also available, but it’s rarely used.
- Since you’re not specifying a connection pool, which we cover later in this chapter, Hibernate uses its own rudimentary connection-pooling mechanism. The internal pool is fine for basic testing, but you shouldn’t use it in production.
- The dialect property tells Hibernate which SQL dialect to use for certain operations. Although not strictly required, it should be used toensure Hibernate Query Language (HQL) statements are correctly converted into the proper SQL dialect for the underlying database.
- Mapping definitions for persistent objects may be stored together in asingle mapping file. since storing the definitions for a large number of persistent classes in one mapping file can be cumbersom. If you have all mapping definitions in a single file, it may be hard to debug and isolate any error to a specific class definition.
No comments:
Post a Comment