1 / 24

Java - HIBERNATE

Java - HIBERNATE. Przygotowali: Łukasz Monkiewicz, Łukasz Zawadzki. Plan prezentacji. Co to jest Hibernate?? Podstawowe cechy Hibernate Dlaczego Hibernate jest wartościową technologią?? Podstawowe elementy rozwijanej aplikacji Użycie złożonych relacji i zapytań, kryteriów, dziedziczenia

pomona
Download Presentation

Java - 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. Java-HIBERNATE Przygotowali: Łukasz Monkiewicz, Łukasz Zawadzki

  2. Plan prezentacji • Co to jest Hibernate?? • Podstawowe cechy Hibernate • Dlaczego Hibernate jest wartościową technologią?? • Podstawowe elementy rozwijanej aplikacji • Użycie złożonych relacji i zapytań, kryteriów, dziedziczenia • XDoclet a HIBERNATE • Porównanie do EJB

  3. Co to jest hibernate?? • Jest to technologia wysokiej wydajności i szybkości działania służąca do wykonywania operacji na obiektowo – relacyjnych bazach danych z poziomu języka Java

  4. Podstawowe cechy technologii Hibernate: • Nie ma potrzeby implementowania interfejsów • Dowolna klasa może być klasą reprezentującą encję • Wygodne i intuicyjne mapowanie plików struktury xml na klasy • Mniej kodu = mniej błędów • Optymalna wydajność

  5. Dlaczego warto nauczyć się Hibernate • Proste zasady działania • Możliwość korzystania z możliwości SQL bez wychodzenia poza granicę języka Java • Możliwość stworzenia dużej bazy bez większego wkładu pracy • Open source • Dostepność narzędzi open source • Popularność

  6. Podstawowe elementy Aplikacji • Klasa reprezentująca encję: • Posiada metody get i set dla atrybutów klasy – pól w tabeli: Class Entity { int id; private void setId(int id) { this.id = id; } public int getId() { return id; } }

  7. Podstawowe elementy Aplikacji c.d. • Posiada bez argumentowy konstruktor. • Plik mapowania(Entity.hbm.xml): <hibernate-mapping> <class name="Entity" table=„Entity"> <id name="id" column="ID"> <generator class="increment"/> </id> <property name="date" type="timestamp" column="DATE"/> <property name="title"/> </class> </hibernate-mapping>

  8. Podstawowe elementy Aplikacji c.d. • Plik konfiguracji (hibernate.properties lub hibernate.cfg.xml) : <hibernate-configuration> <session-factory> <!-- Database connection settings --> <property name="connection.driver_class"> org.hsqldb.jdbcDriver </property> <property name="connection.url"> jdbc:hsqldb:data/tutorial </property> <property name="connection.username"> sa </property> <property name="connection.password"> </property> <property name="dialect">org.hibernate.dialect.HSQLDialect </property> <property name="show_sql"> true </property> <mapping resource="Entity.hbm.xml"/> </session-factory> </hibernate-configuration>

  9. Podstawowe elementy Aplikacji c.d. • Przykładowykod wykonywalny: class EntityManager { public static void main(String[] args) { EntityManager em = new EntityMenager(); em.createAndStoreSomething(„nazwa”, new Date()); } }

  10. Podstawowe elementy Aplikacji c.d. • Przykładowykod wykonywalny: public void createAndStoreSomething(String title, new Date()) { public static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); public static final ThreadLocal session = new ThreadLocal(); Session s = (Session) session.get(); if( s == null) { s = sessionFactory.openSession(); session.set(s); } Transaction tx = s.beginTransaction(); Entity ent = new Entity(); ent.setTitle(title); ent.setDate(theDate); s.save(theEvent); tx.commit(); s.close(); session.set(null); }

  11. Podstawowe elementy Aplikacji c.d. • Przedstawienie relacji między tabelami: _________________ _________________ | PERSON | | Entity | |_______________ | | _______________ | | person_id | * 0..1| ID | | name | ----------------------------------| title | | | | date | | | | | | | | | |_______________ | |________________|

  12. Podstawowe elementy Aplikacji c.d. • Klasa Person.java class Person { int person_id; String name; Entity ent; //metody get i set dla atrybutów… public Entity getEntity() { return ent; } public void setEntity(Entity ent) { this.ent = ent; } }

  13. Podstawowe elementy Aplikacji c.d. • Person.hbm.xml : <hibernate-mapping> <class name=„Person" table=„Persons"> <id name=„person_id" column=„PERSON_ID"> <generator class="increment"/> </id> <property name=„name”/> • <many-to-one name=„ent„ column="ID" class=„Entity„ not-null="true"/> </class> </hibernate-mapping>

  14. Podstawowe elementy Aplikacji c.d. • Klasa Entity.java : class Entity { private Set persons = new HashSet(); … public void setPersons(Set persons) { this.persons = persons; } public Set getPersons() { return persons; } }

  15. Podstawowe elementy Aplikacji c.d. • Entity.hbm.xml : <hibernate-mapping> <class name="Entity" table=„Entity"> … <set name=„persons"> <key column=„ID"/> <one-to-many class=„Person"/> </set> </class> </hibernate-mapping>

  16. Podstawowe elementy Aplikacji c.d. • Przykładowy kod wykorzystujący relację: class EntityManager { public static void main(String[] args) { public static final SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); public static final ThreadLocal session = new ThreadLocal(); Session s = (Session) session.get(); if( s == null) { s = sessionFactory.openSession(); session.set(s); } Transaction tx = s.beginTransaction(); Person aPerson = (Person) session.load(Person.class, 2); Entity anEntity = (Entity) session.load(Event.class, 1); anEntity.getPersons().add(aPerson); tx.commit(); s.close(); session.set(null); } }

  17. Złożone zapytania List mothers = session.createQuery( "select mother from Cat as cat join cat.mother as mother where cat.name = ?") .setString(0, name) .list(); List kittens = session.createQuery( "from Cat as cat where cat.mother = ?") .setEntity(0, pk) .list(); Cat mother = (Cat) session.createQuery( "select cat.mother from Cat as cat where cat = ?") .setEntity(0, izi) .uniqueResult();

  18. Kryteria List cats = sess.createCriteria(Cat.class) .add( Restrictions.like("name", "Fritz%") ) .add( Restrictions.or( Restrictions.eq( "age", new Integer(0) ), Restrictions.isNull("age") ) ) .list(); List cats = sess.createCriteria(Cat.class) .add( Restrictions.sql( "lower({alias}.name) like lower(?)", "Fritz%", Hibernate.STRING ) ) .list();

  19. public class Person { String name; Long id; ... // getter, setter } public class Employee extends Person { String NIP; ... // getter, setter } Dziedziczenie

  20. Dziedziczenie <class name="Person" table="persons" discriminator-value="P"> <id name="id"> <generator class="native"/> </id> <discriminator column="subclass" type="character"/> <property name="name" type="string"/> <subclass name="Employee" discriminator-value=„E"> <property name="NIP" type="string"/> </subclass> </class>

  21. <class name="Person"> <id name="id" column="personId"> <generator class="native"/> </id> <set name="addresses"> <key column="personId"/> <many-to-many column="addressId" class="Address"/> </set> </class> <class name="Address"> <id name="id" column="addressId"> <generator class="native"/> </id> <set name="people" inverse="true"> <key column="addressId"/> <many-to-many column="personId" class="Person"/> </set> </class> Relacje: wiele do wiele

  22. XDoclet i Hibernate /** * @hibernate.class * table="Person" */ public class Person { private Long id; private String name; /* * @hibernate.id * generator-class="native" * column="PERSON_ID" */ public Long getId() { return id;} private void setId(Long id) { this.id=id;} /* * @hibernate.property * column="NAME" */ public String getName() { return id;} private void setName(Name name) { this.name=name;} }

  23. Mniej zbędnego kodu Nie wymaga serwera aplikacji Wydajność Rozbudowany język zapytań Porównanie do EJB

  24. Podsumowanie • Technologia popularna ( ok. 1500 pobrań dziennie ) • Prosta implementacja a efekt działania aplikacji • Więcej na www.hibernate.org • Literatura: „Hibernate in action” – wyd. Manning

More Related