220 likes | 311 Vues
Understand ORM concepts, mapping with Hibernate, annotations vs XML, create-retrieve-update-delete operations, JPA Query Language. Compare classes to tables, explore different mapping ways, and learn query language examples.
E N D
ORM with JPA/Hibernate overview Petar Milev
Contents • ORM Concepts • Java class to database table • Java object to database table’s row • Classes vs. tables • (OR) Mapping with Hibernate • Two ways to go • Annotations vs. XML • Create, Retrieve, Update, Delete and JPA QL
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 class Person{ Long id; String name; String description; //getter, setters}
Instances Are Rows • Java instances are represented as rows in the table void mymethod(Person p){ System.out.println(p.getId()); //1 System.out.println(p.getName()); //’Pesho’ System.out.println(p.getDescription());//’Java man’ }
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
Ways To Go - XML • XML files for every mapped class <class name=“www.com.Person" table=“PERSON"> <id column=“ID" name="id" type="java.lang.Long“/> <property column=“NAME" name=“name" type="java.lang.String"/><property column=“DESCRIPTION" name=“description" type="java.lang.String"/> <set name=“hairs"> <key column=“PERSON_ID" not-null="true"/><one-to-many class=“www.com.Hair"/> </set></class>
Ways To Go – Java Annotations • Java Annotations used at the mapped classes @Entity(“PERSON”) public class Person { private Long id; private String name; private String description; private Set<Hair> hairs; //..code continued
Java Annotations 2 • Java Annotations used at the mapped classes //... @Id public Long getId() { return id; } @Column(name=“NAME”) public String getName() { return name; } @Column(name=“DESCRIPTION”) public String getDescription(){ return description; } @OneToMany(mappedBy=“person”) public Set<Hair> getHairs(){ return hairs; } //normal setters }
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
XML vs. Annotations • XML advantages • Java code is more readable • XML shortcomings • More things to write • Support simultaneously two files: .xml and .java
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
(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();}
CR(UD) • Update void update(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); person.setDescription(“new description”); …. entityManager.getTransaction().commit(); } • Delete void delete(EntityManager entityManager){ Person person = entityManager.find(Person.class, 1); entityManager.remove(person); }
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’
More Query Examples • 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 ) • Projection select a, b from Animal a, Banica b; //returns List of Object[], the array is with 2 elements
More Query Examples • Implicit joins from Bet bet where bet.item.category.name like ‘hundred%' and bet.item.firstBet.quantity > 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 > 100
More Query Examples • Normal joins from Person p left join p.hairs h with h.length > 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