To help make application code less verbose, Hibernate provides some shortcut methods on the Session interface that let you invoke a query in one line of code. The Criteria interface is very similar it allows you to create and execute object-oriented criteria queries. A Query instance is used to bind query parameters, limit the number of results returned by the query, and finally to execute the query. Queries are written in HQL or in the native SQL dialect of your database. The Query interface allows you to perform queries against the database and control how the query is executed. This helps to keep Hibernate applications portable between different kinds of execution environments and containers. A Transaction abstracts application code from the underlying transaction implementation-which might be a JDBC transaction, a JTA UserTransaction, or even a Common Object Request Broker Architecture (CORBA) transaction-allowing the application to control transaction boundaries via a consistent API. Hibernate applications may choose not to use this interface, instead managing transactions in their own infrastructure code. The Transaction interface is an optional API. The application uses a Configuration instance to specify the location of mapping documents and Hibernate-specific properties and then create the SessionFactory.Įven though the Configuration interface plays a relatively small part in the total scope of a Hibernate application, it's the first object you'll meet when you begin using Hibernate. The Configuration object is used to configure and bootstrap Hibernate. It also holds cached data that has been read in one unit of work and may be reused in a future unit of work (only if class and collection mappings specify that this second-level cache is desirable). The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. However, if your application accesses multiple databases using Hibernate, you'll need a SessionFactory for each database. There is typically a single SessionFactory for the whole application-created during application initialization, for example. The SessionFactory is certainly not lightweight! It's intended to be shared among many application threads. Compared to the Session interface, this object is much less exciting. The application obtains Session instances from a SessionFactory. We sometimes use user session to refer to the HttpSession object. When we use the word session, we mean the Hibernate session. Note that a Hibernate session has nothing to do with the Web-tier HttpSession. We sometimes call the Session a persistence manager because it's also the interface for persistence-related operations such as storing and retrieving objects. Hibernate can detect changes to the objects in this unit of work. It may be easier to think of a session as a cache or collection of loaded objects relating to a single unit of work. The Hibernate notion of a session is something between connection and transaction. Hibernate sessions are not thread-safe and should by design be used by only one thread at a time. This is important because your application will need to create and destroy sessions all the time, perhaps on every request. An instance of Session is lightweight and is inexpensive to create and destroy. The Session interface is the primary interface used by Hibernate applications.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |