Download presentation
Presentation is loading. Please wait.
Published byLizbeth Nicholson Modified over 9 years ago
1
Hibernate Basics 廖峻鋒 Sep 14,2004 NTU Dept. of CSIE
2
國立政治大學電子計算機中心 What is Persistence ? Ability of an object to survive even current session or program terminate. Source : W.Keller “Persistence Options for Object-Oriented Programs”, JAOO 2003
3
國立政治大學電子計算機中心 Object-Relational Mapping Automated persistence of object to tables in RDBMS. Usually with the help of metadata that describes the mapping. SQL is auto-generated by the metadata description.
4
國立政治大學電子計算機中心 ORM Solution An ORM Solution consists of the following pieces: Persistence Manager with CRUD API. Query API Mapping metadata Other cross-cutting concerns : transaction, lazy fetching, catching …
5
國立政治大學電子計算機中心 ORM Quality [Fussel 97] Pure relational 整個應用程式以 table-oriented 方式來設計 Light object mapping Using DAO pattern to hide SQL/JDBC from object model. Medium object mapping Simple O-R mapping framework, ex: iBATIS SQLMap Full object mapping Supports sophisticated object modeling : composition, inheritance, persistence by reachability, ex : hibernate
6
國立政治大學電子計算機中心 Generic ORM Problems (1) How persistence classes look like ? 需要 extends 特殊界面嗎 ? 需要 implements serializable 嗎 ? 建構子能不能有參數 ? How mapping metadata defined ? How to map inheritance hierarchies ? How to map object identity, equality as well as database identity.
7
國立政治大學電子計算機中心 Generic ORM Problems (2) How business logic interact with persistence logic ? How to manage lifecycle of entities ? How to provide sorting, searching and aggregating ? How to retrieve data efficiently ?
8
國立政治大學電子計算機中心 “Modern” ORM Solutions Transparent Persistence (POJO/JavaBeans) Persistent/transient instances Automatic Dirty Checking Transitive Persistence Lazy Fetching Outer Join Fetching Runtime SQL Generation Three Basic Inheritance Mapping Strategies
9
國立政治大學電子計算機中心 Hibernate Open Source (LGPL) Popular (13 000 downloads/month) Persistence for JavaBeans Support for very fine-grained, richly typed object models Powerful queries (Criteria and HQL)
10
國立政治大學電子計算機中心 Hibernate Architecture
11
國立政治大學電子計算機中心 Four types of Interfaces Perform or support CRUD and query : Session,Transaction, Query. Config : SessionFactory, Configuration. Callback interface : Interceptor, Lifecycle, Validatable. (not covered in this slide) Extension point : UserType, IdentifierGenerator. (not covered in this slide) Please refer to Hibernate in Action chap 8 for details of callback and extension interfaces
12
國立政治大學電子計算機中心 Core Interfaces Session SessionFactory Configuration Query and Criteria
13
國立政治大學電子計算機中心 Session Persistence Manager of hibernate. One for each thread – can not be shared, not thread safe. Light weight – inexpensive to create / destroy. A cache of loaded objects related to a single unit of work (I.e. a transaction).
14
國立政治大學電子計算機中心 SessionFactory Heavy weight, intended to be shared among threads – typically single SessionFactory for whole application. One SessionFacotry per Database.
15
國立政治大學電子計算機中心 Configuration Specify the location of mapping document. Store hibernate properties. Create SessionFactory. Configuration cfg = new Configuration(); cfg.addClacc(UserInfo.class); SessionFactory factory = cfg.buildSessionFactory();
16
國立政治大學電子計算機中心 Hibernate Mapping metadata One mapping file per class. Naming convention : (entity class name).hbm.xml Usually put in the same directory with class files Otherwise you have to specify metadata files in Configuration using addRecourse() method.
17
國立政治大學電子計算機中心 Implementation (for new Application) Write mapping document. Generate class files and create tables (using Ant) Set hibernate.properties. Implement access code with DAO pattern.
18
國立政治大學電子計算機中心 Implementation (if tables pre-exist) Write class files. Write mapping document. Set hibernate.properties. Implement access code with DAO pattern.
19
國立政治大學電子計算機中心 Persistence Class in Hibernate JavaBean specification (or POJOs) No-arg constructor Accessor methods for properties
20
國立政治大學電子計算機中心 A quick example public class PersistableMessage { private Integer id; private String text; private PersistableMessage next; … (getter and setter methods) … }
21
國立政治大學電子計算機中心 Hibernate.properties Should be put in the classpath. Connection pool -hibernate 內建 support 三種 connection pool : C3p0, apache DBCP, and Proxool
22
國立政治大學電子計算機中心 Writing Metadata
23
國立政治大學電子計算機中心 ID generating strategies
24
國立政治大學電子計算機中心 Retrieving Objects Hibernate Query Language (HQL) “Minimal” OO dialect of ANSI SQL Criteria Queries Extensible framework for expressing query criteria as objects Includes “query by example” Native SQL Queries (NamedQuery)
25
國立政治大學電子計算機中心 Hibernate Query Language Example: select item from AuctionItem item join item.bids bid where item.description like ‘hib%’ and bid.amount > 100 i.e. get all the AuctionItem s with a Bid worth > 100 and description that begins with “hib”
26
國立政治大學電子計算機中心 Criteria Queries Criteria criteria = session.createCriteria(PersistableMessage.class); criteria.add(Expression.eq("id", Integer.valueOf("1"))); PersistableMessage result = (PersistableMessage) criteria.uniqueResult();
27
國立政治大學電子計算機中心 demo Code generation with hbm2java Auto-import with hbm2ddl Client code Test add message with transaction
28
國立政治大學電子計算機中心 Fine-grained Persistence “More classes than tables” Fine-grained object models are good Greater code reuse More typesafe Better encapsulation
29
國立政治大學電子計算機中心 Dependent Mapping Entity has its own database identity (primary key). Value is owned by an entity, its lifecycle dependents on owning entity. Hibernate use to map value type properties.
30
國立政治大學電子計算機中心 Inheritance Mapping
31
國立政治大學電子計算機中心 Three ways to map Inheritance Single Table Inheritance Concrete Table Inheritance Class Table Inheritance
32
國立政治大學電子計算機中心 Single Table Inheritance Map all the classes of an inheritance hierarchy to a single table
33
國立政治大學電子計算機中心 Single Table Inheritance in Hibernate 以 discriminator 欄位區分是那一個子類別 在父類別中定義 discriminator 在子類別中定義 discriminator value
34
國立政治大學電子計算機中心 Fine-grained object model : An example A more complex example : CACS membership management system.
35
國立政治大學電子計算機中心 Topics not covered Advanced associations mapping Transaction / concurrency
36
國立政治大學電子計算機中心 Mapping Association One-to-one Many-to-one One-to-many Unidirectional / bidirectional
37
國立政治大學電子計算機中心 Q&A
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.