Title: IT Boxing Championship
1ORM with JPA/Hibernate overview
Petar Milev
2Contents
- ORM Concepts
- Java class to database table
- Java object to database tables row
- Classes vs. tables
- (OR) Mapping with Hibernate
- Two ways to go
- Annotations vs. XML
- Create, Retrieve, Update, Delete and JPA QL
3ORM Concepts
4Class To Table
- Associate Java POJO with database table
- Table PERSON (SQL may be generated by Hibernate)
- You can do it without even using SQL
ID NAME DESCRIPTION
1 Pesho Java man
2 Gosho Just man
class Person Long id String name String
description //getter, setters
5Instances Are Rows
- Java instances are represented as rows in the
table
ID NAME DESCRIPTION
1 Pesho Java man
2 Gosho Just man
void mymethod(Person p) System.out.println(p.g
etId()) //1 System.out.println(p.getName())
//Pesho System.out.println(p.getDescription()
)//Java man
6Classes vs. Tables
- Classes inherit from each other
- Classes have associations between them
- What about tables?
- The differences lead to the need for meta
information -
7(OR) Mappings
8Ways To Go - XML
- XML files for every mapped class
ltclass namewww.com.Person" tablePERSON"gt
ltid columnID" name"id" type"java.lang.Long/gt
ltproperty columnNAME" namename"
type"java.lang.String"/gt ltproperty
columnDESCRIPTION" namedescription"
type"java.lang.String"/gt ltset
namehairs"gt ltkey columnPERSON_ID"
not-null"true"/gt ltone-to-many
classwww.com.Hair"/gt lt/setgtlt/classgt
9Ways To Go Java Annotations
- Java Annotations used at the mapped classes
_at_Entity(PERSON) public class Person
private Long id private String name
private String description private SetltHairgt
hairs //..code continued
10Java Annotations 2
- Java Annotations used at the mapped classes
//... _at_Id public Long getId()
return id _at_Column(nameNAME) public
String getName() return name
_at_Column(nameDESCRIPTION) public String
getDescription() return description
_at_OneToMany(mappedByperson) public
SetltHairgt getHairs() return hairs
//normal setters
11Annotations vs. XML
- Annotations advantages
- Everything is in one place
- Annotations are refactoring-friendly for renaming
and moving of classes - Annotations are controlled by the compiler
- Annotations shortcomings
- Cluttered source code
12XML vs. Annotations
- XML advantages
- Java code is more readable
- XML shortcomings
- More things to write
- Support simultaneously two files .xml and .java
13More about mappings in JPA
- Configuration by exception
- You can tell JPA how to fetch the relations
- Transient properties
- Map relations
- Map maps
- Embedded objects
- Compound primary keys
14Create, Read, Update, Delete
15(CR)UD
void create(EntityManager entityManager)
Person person new Person(pesho, java
programmer) entityManager.persist(person)
void read(EntityManager entityManager)
Person person entityManager.find(Person.class,
1)
void query(EntityManager entityManager)
Query query entityManager.createQuery(from
Person where name like pesho ) List
persons query.getResultList()
16CR(UD)
void update(EntityManager entityManager)
Person person entityManager.find(Person.class,
1) person.setDescription(new
description) . entityManager.getTransac
tion().commit()
void delete(EntityManager entityManager)
Person person entityManager.find(Person.class,
1) entityManager.remove(person)
17Query Language Examples
from Shape //where Shape is an abstract
class from java.lang.Object //get the whole
database, why not?
- Where, like, in, functions
from Person p where p.name like P and
p.description like J or p.name in (John,
Ben, Petar) or lower(p.name) pesho
18More Query Examples
- Sub selects with any, some, all and in
from Box b where 10 lt any ( select b.amount from
i.bets b ) from Box b where 10 gt 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 )
select a, b from Animal a, Banica b //returns
List of Object, the array is with
2 elements
19More Query Examples
from Bet bet where bet.item.category.name like
hundred' and bet.item.firstBet.quantity gt 10
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 gt 100
20More Query Examples
from Person p left join p.hairs h with h.length gt
10 where p.description like 'Foo'
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