IT Boxing Championship - PowerPoint PPT Presentation

About This Presentation
Title:

IT Boxing Championship

Description:

Ways To Go - XML. XML files for every mapped class class name ... You can tell JPA how to fetch the relations. Transient properties. Map relations. Map maps ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 21
Provided by: itboxingc
Learn more at: https://www.devbg.org
Category:

less

Transcript and Presenter's Notes

Title: IT Boxing Championship


1
ORM with JPA/Hibernate overview
Petar Milev
2
Contents
  • 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

3
ORM Concepts
4
Class 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
5
Instances 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
6
Classes 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
8
Ways 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
9
Ways 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
10
Java 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
11
Annotations 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

12
XML vs. Annotations
  • XML advantages
  • Java code is more readable
  • XML shortcomings
  • More things to write
  • Support simultaneously two files .xml and .java

13
More 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

14
Create, Read, Update, Delete
15
(CR)UD
  • Create
  • Read

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)
  • Query

void query(EntityManager entityManager)
Query query entityManager.createQuery(from
Person where name like pesho ) List
persons query.getResultList()
16
CR(UD)
  • Update

void update(EntityManager entityManager)
Person person entityManager.find(Person.class,
1) person.setDescription(new
description) . entityManager.getTransac
tion().commit()
  • Delete

void delete(EntityManager entityManager)
Person person entityManager.find(Person.class,
1) entityManager.remove(person)
17
Query Language Examples
  • Polymorphic queries

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
18
More 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 )
  • Projection

select a, b from Animal a, Banica b //returns
List of Object, the array is with
2 elements
19
More Query Examples
  • Implicit joins

from Bet bet where bet.item.category.name like
hundred' and bet.item.firstBet.quantity gt 10
  • SQL 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 gt 100
20
More Query Examples
  • Normal joins

from Person p left join p.hairs h with h.length gt
10 where p.description like 'Foo'
  • Dynamic 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
Write a Comment
User Comments (0)
About PowerShow.com