30
Seminar on

Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Embed Size (px)

Citation preview

Page 1: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Seminar on

Page 2: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Overview

Hibernate. What is it?

Hibernate. How does it work?

Hibernate Tools

Page 3: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate. What is it?

Page 4: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Definition

Hibernate is free as open source software

Hibernate is an ORM library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database

Page 5: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Definition

Page 6: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Definition

Page 7: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Creator

Gavin King [email protected] The author of the book «Hibernate in Action»

together with Christian Bauer

Page 8: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

The Goals

To provide automated & optimized techniques

To reduce the time that developers devote to manual coding & make them concentrated more on business logic of their application (25%)

To insulate two object models To provide smart fetching & cashing To supply a toolset for development

Page 9: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Benefits

Provides quite simple model (fewer LOC)

Rather easy for learning

Open source software

DBMS independence

Own query language (HQL)

Page 10: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate.How does it work?

Page 11: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

All you need for start

Hibernate distribution (hibernate.org) Database of your choice (hsqldb preferably) SQL tables to hold your persistent objects Java classes to represent that objects in

code XML mapping files Configuration file for database connection

settings

Page 12: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Development strategies

Java → Hibernate → Database (top-down)

Java ← Hibernate ← Database (bottom-up)

Java ← Hibernate → Database (middle-out)

Page 13: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Persistent Class

package hello;

private class Message{ private Long id; private String text; private Message nextMessage; private Message() {} public Message(String text){ this.text = text; } public Long getId(){ return id; }

Page 14: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Persistent Class

public void setId(Long id){ this.id = id; } public String getText(){ return text; } public void setText(String text){ this.text = text; } public Message getNextMessage(){

return nextMessage; } public void Message(Message nextMessage){ this.nextMessage = nextMessage; } }

Page 15: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

XML Mapping

Tells Hibernate what classes to store in a database and how they relate to Tables and Columns

Instead of mappings you can use annotated classes

Page 16: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

XML Mapping

<hibernate-mapping><class name=“hello.Message”

table=“MESSAGES”><id name=“id” column=“MESSAGE_ID”> <generator class=“increment”/> (assigned, natural)

</id><property name=“text” column=“MESSAGE_TEXT”/><many-to-one

name=“nextMessage”cascade=“all”column=“NEXT_MESSAGE_ID”/>

</class></hibernate-mapping>

Page 17: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Database settings (.cfg.xml)

<hibernate-configuration><session-factory> <property

name="hibernate.connection.driver_class">org.hsqldb.jdbcDriver</property>

<property name="hibernate.connection.url">jdbc:hsqldb:hsql://localhost/

</property> <property

name="hibernate.connection.username">sa</property>

<property name="dialect">org.hibernate.dialect.HSQLDialect</property>

<mapping resource="hello/Message.hbm.xml"/> </session-factory>

</hibernate-configuration>

Page 18: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Create Database Table

Start the database Create a table Now it’s time to see Hibernate in action

Page 19: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Work with objects

Page 20: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Work with objects

Through the Session Hibernate works with a Database

Transaction involves a unite of work with a Database

Page 21: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Work with objects

Page 22: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate Tools

Page 23: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate Tools

Toolset for development Makes working with Hibernate much easier Comes with JBoss Tools A part of JBDS Available both as plugin to Eclipse or via Ant

tasks

Page 24: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Shortly about Ant task

To start using Hibernate Tools via Ant define the fallowing task:

<taskdef name="hibernatetool"

classname="org.hibernate.tool.ant.HibernateToolTask"

classpathref="project.classpath"/>

Page 25: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate plugin within Eclipse

Provides a number of wizards & editors for smart & simpler work with Hibernate

Allows to watch the structure of mapping files, execute queries as well as see the results

All of these features are available through the Hibernate perspective

Page 26: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Development Wizards

for creating:

Hibernate Configuration File (.cfg.xml) Hibernate XML mapping file (.hbm.xml) Hibernate Console Configuration Hibernate Reverse Engineering (reveng.xml)

Page 27: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Code Generation

Make use of it when you need to generate one kind of artifacts to another

Note :

while generating hbm.xml’s, not all conversions might be implemented, thus some hand code editing can be necessary.

Page 28: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Editors

Hibernate Configuration file editor Hibernate Mapping file editor Reveng.xml editor

Page 29: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Hibernate Console Perspective

Hibernate Configuration view Mapping Diagram HQL or Hibernate Criteria editors Hibernate query result view & Dynamic SQL

translator view Properties View

Page 30: Seminar on. Overview Hibernate. What is it? Hibernate. How does it work? Hibernate Tools

Gratitude

Thanks a lot Vitalii Yemialyanchyk &

Geraskov Dmitrii

Presentation is made with help of the book

“Hibernate in action” & “Hibernate Tools

Reference Guide”