130 likes | 143 Views
This article provides an overview of Hibernate and the Elver-Hibernate architecture, along with a sample source code. It also discusses the current TPTP database support and the challenges of persistence. The solution involves using Hibernate for object-relational mapping and Elver for EMF support. The article includes code samples for creating a data store, executing SQL and HQL queries, and creating and loading a resource. It also highlights the benefits of Elver, such as multi-database support, automatic schema generation, exception handling, and annotations support.
E N D
Database support for TPTP EMF based data models using Elver/Hibernate Guru Nagarajan July 14, 2006
Outline • What is Hibernate? • Overview of the Elver-Hibernate architecture • Source Sample • Current TPTP database support • Questions
What is the Problem statement? • EMF objects can be persisted in relational databases and retrieved. The persistence and retrieval should support • Most used Relational Databases • Support the retrieval of the EMF objects using the EMF Resource approach • Capitalize on the query and caching capabilities of Relational Databases and use them in building the required EMF Objects
Challenges of Persistence • In-Memory data and Persistence must be kept in sync • The Object Model requirements could change and the persistence data store definition must be modified • Portability of the persistence is an item not to be ignored • Object-Relation Mapping to be seamless • Solution: Application should not need to know that its data is being persisted
Your App Object Hibernate Mapping Database What is Hibernate? • Basically, a Object-Relational Mapping tool • Maps Objects -> Relational Databases • Implements those Mappings • Knows about the underlying Database and provides an elegant abstraction • How does Hibernate work? • At runtime, Hibernate reads the mapping and dynamically builds Java classes that manages database-object interaction. • Provides an API to manage, query the objects from the database, and create objects
EMF Model Database What is Elver? • Elver basically adds the EMF support to Hibernate OR • It consists of primarily two components • Mapping Tier • Support for EMF annotations • Runtime Tier • Query, Save, Create objects using EMF Object Hibernate Mapping EMF Datastore
Elver Code Sample • // Create the DataStore. final String dataStoreName = “YourModelDataStore"; final HbDataStore dataStore = HbHelper.INSTANCE.createRegisterDataStore(dataStoreName); • // Configure the EPackages used by this DataStore. dataStore.setEPackages(new EPackage[] { EMFPackagePackage.eINSTANCE }); • // Initialize the DataStore. This sets up the Hibernate mapping and creates the corresponding tables in the database. dataStore.initialize(); • //start a transaction final SessionFactory sessionFactory = dataStore.getSessionFactory(); // Open a new Session and start Transaction. Session session = sessionFactory.openSession(); session.beginTransaction(); • Create and populate Model Model model = ModelFactory.eINSTANCE.createModel(); //Set Model Properties // Make it persistent. session.save(model); Commit Transaction and close Session • session.getTransaction().commit(); session.close();
Sample – Creating SQL & HQL Queries, Resource • SQL Query • query = session.createQuery("SELECT count(allbooks) FROM Model model WHERE <<>>=<<>>"); • Hibernate Query (HQL) • Query query = session.createQuery("FROM Model.member"); • List modelmember = query.list(); • Creating and Loading Resource String uriStr = "hibernate://?" + YourSessionFactory"; final URI uri = URI.createURI(uriStr); ResourceSet resourceSet = new ResourceSetImpl(); final Resource res = resourceSet.createResource(uri); • //Load the resource res.load(Collections.EMPTY_MAP); Iterator it = res.getContents().iterator(); //Iterate through the iyterator to get your model objects
TPTP Database Support - Overview • TPTP database interface is very similar to Hibernate • Database Interface • TypeMap • Paging … • Query Builder • Object Query – Subset of HQL (Hibernate Query) • Reference Query – EMF Query • Support for Log database schema • For other models you have to extend, provide the schema
Why Elver? • Multi-Database support • MySql, Oracle, DB2, Access etc. • Database specific SQL (SQL Diaelect) supported • Exposes the Database performance via SQL queries (Hibernate ftr) • Automatic Schema Generation • Provide a EMF model, set its characteristics and Elver/Hibernate creates the mapping, schema for runtime • Not “Model” Specific • Exception Handling & Logging • Very good exception handling • Log4J Logging • Annotations Support • Preserve the Annotations in the Database • Database Transaction support
Useful Links • Elver Page: http://www.elver.org • Hibernate: http://www.hibernate.org • License: • Elver: Apache 2.0 • Hibernate: LGPL