310 likes | 585 Views
Seminar on. Overview. Hibernate. What is it? Hibernate. How does it work? Hibernate Tools. Hibernate. What is it?. Definition. Hibernate is free as open source software
E N D
Overview • Hibernate. What is it? • Hibernate. How does it work? • Hibernate Tools
Definition • Hibernate is free as open source software • Hibernateis an ORM library for the Java language, providing a framework for mapping an object-oriented domain model to a traditional relational database
Creator • Gavin King • gavin@hibernate.org • The author of the book «Hibernate in Action» together withChristian Bauer
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
Provides quite simple model (fewer LOC) Rather easy for learning Open source software DBMS independence Own query language (HQL) Benefits
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 mappingfiles • Configuration file for database connection settings
Development strategies • Java →Hibernate→ Database (top-down) • Java ←Hibernate← Database (bottom-up) • Java ←Hibernate→ Database (middle-out)
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; }
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; } }
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
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>
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>
Create Database Table • Start the database • Create a table • Now it’s time to see Hibernate in action
Work with objects • Through the SessionHibernate works with a Database • Transaction involves a unite of work with a Database
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
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"/>
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
Development Wizards for creating: • Hibernate Configuration File (.cfg.xml) • Hibernate XML mapping file (.hbm.xml) • Hibernate Console Configuration • Hibernate Reverse Engineering (reveng.xml)
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.
Editors • Hibernate Configuration file editor • Hibernate Mapping file editor • Reveng.xml editor
Hibernate Console Perspective • Hibernate Configuration view • Mapping Diagram • HQL or Hibernate Criteria editors • Hibernate query result view & Dynamic SQL translator view • Properties View
Gratitude • Thanks a lot Vitalii Yemialyanchyk & Geraskov Dmitrii Presentation is made with help of the book “Hibernate in action” & “Hibernate Tools Reference Guide”