1 / 7

Hibernate

Hibernate. What is Hibernate ? An Object / Relational Mapping (ORM) framework for Java

tameka
Download Presentation

Hibernate

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hibernate • WhatisHibernate ? • An Object/RelationalMapping (ORM) frameworkfor Java • Hibernate let's you develop persistent classes following object-oriented idiom - including association, inheritance, polymorphism, composition and collections. Hibernate allows you to express queries in its own portable SQL extension (HQL), as well as in native SQL, or with an object-oriented Criteria and Example API • Open Source • PrincipalAuthor: Gavin King • Other Major Figure: Christian Bauer • Almost a defacto standard ORM for Java • Current version 4.2

  2. MappingProcess • The processofspecifying the bindingsbetweenanobjectmodel and a database schema • Principalmechanismis via XML mappingfiles • Defacto file nameextensionis: .hbm.xml • Multiple ways to set this up: a single file, one file per class. Best practice is is to use one file per class, with each file placed next to its corresponding class file in the package hierarchy, and loaded as a resource

  3. MappingProcess • Entities • BasicProperties • Components • Associations • Many-To-One • One-To-Many • Many-To-Many • Principalmechanismis via XML mappingfiles • Inheritancemapping • Modelingwithinterfaces

  4. HibernateApp • An HibernateAppwillconsistof: • One or more classes (POJOs) tobepopoulatedfrom/copiedto database tables • A mapping file foreachnamedclassname.hbm.xmlmappingeachto the database tableitcorrespondsto • Configurationfilessuchashibernate.cfg.xml • etc … etc …

  5. Hibernateconfiguration • An exampleof the hibernate.config.xml <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-configurationPUBLIC "-//Hibernate/HibernateConfiguration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <propertyname="hibernate.connection.url"> Jdbc:mysql://localhost:3306/db_gestione_corsi </property> <property name="hibernate.connection.username">root</property> <propertyname="hibernate.connection.password"></property> <propertyname="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="show_sql">true</property> <mappingresource="it/tcweb/config/hibernate/persona.hbm.xml" /> <mappingresource="it/tcweb/config/hibernate/utente.hbm.xml" /> </session-factory> </hibernate-configuration>

  6. Hibernateconfiguration • An exampleof the mapping file utente.hbm.xmland MANY-TO-ONE relation <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <classname="it.tcweb.model.dto.Utente" table="utente" catalog="db_gestione_corsi"> <idname="id" type="java.lang.Long"> <columnname="ID" /> <generatorclass="identity" /> </id> <property name="username" type="string"> <column name="USERNAME" length="20" not-null="true" unique="true" /> </property> <property name="password" type="string"> <column name="PASSWORD" length="20" not-null="true" unique="false" /> </property> <propertyname="profilo" type="string"> <column name="PROFILO" length="20" not-null="true" unique="false" /> </property> <many-to-one name="persona" class="it.tcweb.model.dto.Persona"> <column name="ID_PERSONA" not-null="true" /> </many-to-one> </class> </hibernate-mapping>

  7. Hibernateconfiguration • An exampleof the mapping file persona.hbm.xmland ONE-TO-MANY relation • <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <classname="it.tcweb.model.dto.Persona" table="persona" catalog="db_gestione_corsi"> <idname="id" type="java.lang.Long"> <columnname="ID" /> <generatorclass="identity" /> </id> <property name="nome" type="string"> <column name="NOME" length="30" not-null="true" unique="false" /> </property> <property name="cognome" type="string"> <column name="COGNOME" length="30" not-null="true" unique="false" /> </property> <propertyname="dataNascita" type="java.util.Date"> <column name="data_nascita" length="30" not-null="true" unique="false" /> </property> <set name="utenti" cascade="save-update,delete,delete-orphan" table="utente" inverse="true" > <key column="ID_PERSONA" not-null="true" on-delete="cascade"/> <one-to-manyclass="it.tcweb.model.dto.Utente" /> </set> </class> </hibernate-mapping>

More Related