Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
Hibernate Introduction • Why object/relational mapping? • Solving the mismatch with tools • Basic Hibernate features • Hibernate Query Options • Detached Objects
Hibernate Introduction • The Structural Mismatch. • Java types vs. SQL datatypes • user-defined types (UDT) are in SQL:1999 • current products are proprietary • Type inheritance • no common inheritance model • Entity relationships.
Hibernate Introduction • Why ORM • Structural mapping more robust • Less error-prone code • Optimized performance all the time • Vendor independence
Hibernate Introduction • What do RDBs do well? • Work with large amounts of data • Searching, sorting • Work with sets of data • Joining, aggregating • Sharing • Concurrency (Transactions) • Many applications • Integrity • Constraints • Transaction isolation
Hibernate Introduction • What do RDBs do badly? • Modeling • No polymorphism • Fine grained models are difficult • Business logic • Stored procedures really bad
Hibernate Introduction • The Goal • Take advantage of those things that relational databases do well • Without leaving the language of objects / classes
Hibernate Introduction • Hibernate • Open Source (LGPL) • Mature • Popular (15,000 downloads/month) • Custom API • Will be core of JBoss CMP 2.0 engine
Hibernate Introduction • Features • Persistence for POJOs (JavaBeans) • Flexible and intuitive mapping • Support for fine-grained object models • Powerful, high performance queries • Dual-Layer Caching Architecture (HDLCA) • Toolset for roundtrip development • Support for detached persistent objects
Conception • SessionFactory • A thread safe (immutable) cache of compiled mappings for a single database. A factory for Session and a client of Connection Provider • Session • A single-threaded, short-lived object representing a conversation between the application and the persistent store. Wraps a JDBC connection. Factory for Transaction. • Transaction • A single-threaded, short-lived object used by the application to specify atomic units of work. Abstracts application from underlying JDBC, JTA or CORBA transaction. A Session might span several Transactions in some cases • Persistent Objects and Collections • Short-lived, single threaded objects containing persistent state and business function. These might be ordinary JavaBeans/POJOs, • Transient Objects and Collections • Instances of persistent classes that are not currently associated with a Session. • .
Typical Usage Session sess = factory.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); // do some work ... tx.commit(); } catch (Exception e) { if (tx!=null) tx.rollback(); throw e; } finally { sess.close(); }
Hibernate Mapping • Single Table Example 1. PO(Persistent Object) layer: three components: 1) PO java class 2) hbm xml file 3) hibernate.cfg xml file 2. DAO(Data Access Object) layer one java client, having select, insert, delete, update.
Hibernate Mapping • Persistent Class • JavaBean specification (or POJOs) • No-arg constructor • Accessor methods for properties • Identifier property package com.ascent.po; import java.io.Serializable; public abstract class AbstractStudent implements Serializable { private java.lang.Integer id; private java.lang.String name; public AbstractStudent(){} public java.lang.Integer getId() { return id;} public void setId(Integer id) { this.id = id;} } public java.lang.String getName() { return this.name;} public void setName(String name) { this.name = name; }
Hibernate Mapping • Persistent Class(Cont’) package com.ascent.po; import java.io.Serializable; public class Student extends AbstractStudent implements Serializable { public Student() { } public Student(java.lang.Integer id) { super(id); } /* Add customized code below */ }
Hibernate Mapping • DAO class cont’ • //Transaction Test 2 • try{ • Student my_hibernate = new Student(); • //my_hibernate.setId(1); • my_hibernate.setName("ggjj"); • s.save(my_hibernate); • int a=s.delete("from Student where id=14"); • System.out.println(a); • if(a==0) t.rollback(); • s.flush(); • t.commit(); • }catch(HibernateException e) { • t.rollback(); • e.printStackTrace(); • }
Object state • Transient Objects Objects instantiated using the new operator aren’t immediately persistent. Their state is transient, which means they aren’t associated with any database table row, and so their state is lost as soon as they’re dereferenced. • Persistent Objects A persistent instance is any instance with a database identity. Persistent instances are associated with the persistence manager. Persistent instances are always associated with a Session and are transactional • Detached Objects Instances lose their association with the persistence manager when you close() the Session. We refer to these objects as detached, indicating that their state is no longer guaranteed to be synchronized with database state; they’re no longer under the management of Hibernate.
Hibernate Introduction • Layer Communication • The presentation layer is decoupled from the service layer and business logic: Presentation Layer Remote? DTO? Service Layer Domain Objects
Hibernate Introduction • DTOs are Evil • “Useless” extra LOC • Not objects (no behavior) • Parallel class hierarchies smell • Shotgun change smell • Solution: detached object support
Hibernate Introduction • Detached Object Support • For applications using servlets + session beans • You don’t need to select a row when you only want to update it! • You don’t need DTOs anymore! • You may serialize objects to the web tier, then serialize them back to the EJB tier in the next request • Hibernate lets you selectively reassociate a subgraph! (essential for performance)
Hibernate Mapping • one to one example: Address and Customer 1. DB script: create table CUSTOMER ( ID bigint not null, NAME varchar(15), primary key (ID) ); create table ADDRESS( ID bigint not null, STREET varchar(128), CITY varchar(128), PROVINCE varchar(128), ZIPCODE varchar(6), primary key (ID) );
Hibernate Mapping 3. PO class: public abstract class AbstractAddress implements Serializable { … private Customer customer; public mypack.Customer getCustomer() { return this.customer; } public void setCustomer(mypack.Customer customer) { this.customer = customer; } … public abstract class AbstractCustomer implements Serializable{ … private Address address; public mypack.Address getAddress() { return this.address; } public void setAddress(mypack.Address address) { this.address = address; }…
Hibernate Mapping 4. DAO class: … public void printCustomer(Customer customer) throws Exception{ Address address=customer.getAddress(); System.out.println("Address of "+customer.getName()+" is: " +address.getProvince()+" " +address.getCity()+" " +address.getStreet()); if(address.getCustomer()==null) System.out.println("Can not naviagte from address to Customer."); } ….
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.