Download presentation
Presentation is loading. Please wait.
Published byBarnaby Stephens Modified over 9 years ago
1
ORM with JPA/Hibernate overview Petar Milev
2
ContentsContents 1.ORM Concepts Java class to database tableJava class to database table Java object to database table’s rowJava object to database table’s row Classes vs. tablesClasses vs. tables 2.(OR) Mapping with Hibernate Two ways to goTwo ways to go Annotations vs. XMLAnnotations vs. XML 3.Create, Retrieve, Update, Delete and JPA QL 1.ORM Concepts Java class to database tableJava class to database table Java object to database table’s rowJava object to database table’s row Classes vs. tablesClasses vs. tables 2.(OR) Mapping with Hibernate Two ways to goTwo ways to go Annotations vs. XMLAnnotations vs. XML 3.Create, Retrieve, Update, Delete and JPA QL
3
ORM Concepts
4
Class To Table Associate Java POJO with database tableAssociate Java POJO with database table Table: PERSON (SQL may be generated by Hibernate) You can do it without even using SQLYou can do it without even using SQL Associate Java POJO with database tableAssociate Java POJO with database table Table: PERSON (SQL may be generated by Hibernate) You can do it without even using SQLYou can do it without even using SQL IDNAMEDESCRIPTION 1Pesho Java man 2Gosho Just man class Person{ Long id; String name; String description; //getter, setters }
5
Instances Are Rows Java instances are represented as rows in the tableJava instances are represented as rows in the table IDNAMEDESCRIPTION 1Pesho Java man 2Gosho Just man void mymethod(Person p){ System.out.println(p.getId()); //1 System.out.println(p.getId()); //1 System.out.println(p.getName()); //’Pesho’ System.out.println(p.getName()); //’Pesho’ System.out.println(p.getDescription());//’Java System.out.println(p.getDescription());//’Java man’ man’}
6
Classes vs. Tables Classes inherit from each otherClasses inherit from each other Classes have associations between themClasses have associations between them What about tables?What about tables? The differences lead to the need for meta informationThe differences lead to the need for meta information Classes inherit from each otherClasses inherit from each other Classes have associations between themClasses have associations between them What about tables?What about tables? The differences lead to the need for meta informationThe differences lead to the need for meta information
7
(OR) Mappings
8
Ways To Go - XML XML files for every mapped classXML files for every mapped class
9
Ways To Go – Java Annotations Java Annotations used at the mapped classesJava Annotations used at the mapped classes @Entity(“PERSON”) public class Person { private Long id; private Long id; private String name; private String name; private String description; private String description; private Set hairs; private Set hairs; //..code continued
10
Java Annotations 2 Java Annotations used at the mapped classesJava Annotations used at the mapped classes //... @Id public Long getId() { return id; } public Long getId() { return id; } @Column(name=“NAME”) @Column(name=“NAME”) public String getName() { return name; } public String getName() { return name; } @Column(name=“DESCRIPTION”) @Column(name=“DESCRIPTION”) public String getDescription(){ return description; } public String getDescription(){ return description; } @OneToMany(mappedBy=“person”) @OneToMany(mappedBy=“person”) public Set getHairs(){ return hairs; } public Set getHairs(){ return hairs; } //normal setters //normal setters}
11
Annotations vs. XML Annotations advantagesAnnotations advantages Everything is in one placeEverything is in one place Annotations are refactoring-friendly for renaming and moving of classesAnnotations are refactoring-friendly for renaming and moving of classes Annotations are controlled by the compilerAnnotations are controlled by the compiler Annotations shortcomingsAnnotations shortcomings Cluttered source codeCluttered source code Annotations advantagesAnnotations advantages Everything is in one placeEverything is in one place Annotations are refactoring-friendly for renaming and moving of classesAnnotations are refactoring-friendly for renaming and moving of classes Annotations are controlled by the compilerAnnotations are controlled by the compiler Annotations shortcomingsAnnotations shortcomings Cluttered source codeCluttered source code
12
XML vs. Annotations XML advantagesXML advantages Java code is more readableJava code is more readable XML shortcomingsXML shortcomings More things to writeMore things to write Support simultaneously two files:.xml and.javaSupport simultaneously two files:.xml and.java XML advantagesXML advantages Java code is more readableJava code is more readable XML shortcomingsXML shortcomings More things to writeMore things to write Support simultaneously two files:.xml and.javaSupport simultaneously two files:.xml and.java
13
More about mappings in JPA Configuration by exceptionConfiguration by exception You can tell JPA how to fetch the relationsYou can tell JPA how to fetch the relations Transient propertiesTransient properties Map relationsMap relations Map mapsMap maps Embedded objectsEmbedded objects Compound primary keysCompound primary keys Configuration by exceptionConfiguration by exception You can tell JPA how to fetch the relationsYou can tell JPA how to fetch the relations Transient propertiesTransient properties Map relationsMap relations Map mapsMap maps Embedded objectsEmbedded objects Compound primary keysCompound primary keys
14
Create, Read, Update, Delete
15
(CR)UD(CR)UD CreateCreate ReadRead CreateCreate ReadRead void create(EntityManager entityManager){ Person person = new Person(“pesho”, “java programmer”); Person person = new Person(“pesho”, “java programmer”); entityManager.persist(person); entityManager.persist(person);} void read(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); Person person = entityManager.find(Person.class, 1);} void query(EntityManager entityManager){ Query query = entityManager.createQuery(“from Person where name like ‘pesho’ ”); Query query = entityManager.createQuery(“from Person where name like ‘pesho’ ”); List persons = query.getResultList();} List persons = query.getResultList();} QueryQuery
16
CR(UD)CR(UD) UpdateUpdate void update(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); Person person = entityManager.find(Person.class, 1); person.setDescription(“new description”); person.setDescription(“new description”); …. …. entityManager.getTransaction().commit(); entityManager.getTransaction().commit();} void delete(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); Person person = entityManager.find(Person.class, 1); entityManager.remove(person); entityManager.remove(person);} DeleteDelete
17
Query Language Examples Polymorphic queriesPolymorphic queries from Shape; //where Shape is an abstract class from java.lang.Object; //get the whole database, why not? Where, like, in, functionsWhere, like, in, functions from Person p where p.name like ‘P%’ and p.description like ‘J%’ where p.name like ‘P%’ and p.description like ‘J%’ or p.name in (‘John’, ‘Ben’, ‘Petar’) or p.name in (‘John’, ‘Ben’, ‘Petar’) or lower(p.name) = ‘pesho’ or lower(p.name) = ‘pesho’
18
More Query Examples ProjectionProjection select a, b from Animal a, Banica b; //returns List of Object[], the array is with 2 elements Sub selects with ‘any’, ‘some’, ‘all’ and ‘in’Sub selects with ‘any’, ‘some’, ‘all’ and ‘in’ from Box b where 10 <= any ( select b.amount from i.bets b ) from Box b where 10 > all ( select b.amount from i.bets b ) from Box b where 10 = some ( select b.amount from i.bets b ) from Box b where 10 in ( select b.amount from i.bets b )
19
More Query Examples Implicit joinsImplicit joins from Bet bet where bet.item.category.name like ‘hundred%' and bet.item.firstBet.quantity > 10 SQL alternativeSQL alternative select.... from BET B inner join ITEM I on B.ITEM_ID = I.ITEM_ID inner join CATEGORY C on I.CATEGORY_ID = C.CATEGORY_ID inner join BET SB on I.FIRST_BET_ID = SB.BET_ID where C.NAME like ‘hundred%' and SB.QUANTITY > 100
20
More Query Examples Normal joinsNormal joins from Person p left join p.hairs h with h.length > 10 where p.description like '%Foo%' Dynamic instantiationDynamic instantiation select new BetInfo( bet.item.id, count(bet), avg(bet.amount) ) from Bet bet where bet.current.ammount is null group by bet.current.id
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.