110 likes | 360 Views
Hibernate. What is Hibernate ? An Object / Relational Mapping (ORM) framework for Java
E N D
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
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
MappingProcess • Entities • BasicProperties • Components • Associations • Many-To-One • One-To-Many • Many-To-Many • Principalmechanismis via XML mappingfiles • Inheritancemapping • Modelingwithinterfaces
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 …
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>
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>
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>